Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Today I thought I would show you a WPF Application. WPF is Microsoft's rich desktop application framework, it's a lot like Silverlight. So I have selected WPF Application and I'll leave the name at the default, WpfApplication1 and click OK. In the Solution Explorer I can see how similar it is to Silverlight. There is an App.xaml file and a MainWindow.xaml file. In the Editor I can see the same split view I get in SilverLight, the top-half shows the designer and the bottom-half shows my XAML code.
I am going to create a class and call some code in that class. The name of my class is going to be printer. To add it to my project I will right- click on my project and choose Add>Class. I will name my class Printer and click OK. I am going to create two functions inside this Printer class. Functions in .NET are called Methods. I am going to create an instance method and a static method. First one is going to be called GetPrinterName, so I will say public string GetPrinterName. This is the name of my function, this is the return type from the function and this is the visibility of my function.
Functions are defined and methods are defined with a set of parentheses and then the code goes in a set of curly braces. You can see in Visual Studio that I'm getting a red squiggle underneath the GetPrinterName, that's because not all code paths return a value. Visual Studio is telling me that I need to fix my code by returning a value. The return keyword is used here and then I return the name of the printer, in my case it's going to be HP 540P. Lines of code in C# end with a Semicolon (;).
The second function I am going to create is going to have the static keyword in it and this one is going to return a Boolean value. I'm also going to include a parameter, so inside the set of parentheses I am going to say string printerName. And then I'll put the body of my method in with the two curly braces. Again, I haven't returned a value so I am getting my red squiggle. What I am going to do in my body of code here is I am going to right an if statement to check the value of the incoming printer string. In the body of my code I am going to check the printer name and look for a certain value and then return true or false.
To do that I am going to use an if statement. If statements can be written like this, type-in if. Now look closely at this IntelliSense window that has popped up here, there are a couple of things of interest. First, on the left side of the screen you see if and a little piece of paper icon there that piece of paper icon represents that this is a code snippet. On the right side of the screen a few seconds ago there was a tool tip that had popped-up that gave me a little more details about the if statement. To use the code snippet I use the Visual Studio keystrokes, which is Tab Tab. So I'll press Tab twice and it stubs in some code for me.
It put the Open and Close parentheses and the Open and Close curly braces and the word true. I will replace the word true with my logic which is printerName==hard-coded string "HP 540P". So what this says, if the inbound printer parameter is equal to this string then return true, otherwise on an else case down here, else return false.
There are other code snippets available, like for instance I can come down here and take-in for each. Notice I haven't finished typing the word for each, I have only typed the first four letters, that's enough for the IntelliSense engine to recognize it and now if I press the Tab Tab keystrokes it would stub that Code Snippet in for me. All right, I am ready to use this in my main window. I am going to switch back to my main window, I will do that by going to MainWindow.xaml up here in this tab. Now I need some UI over here, I am going to put a button on this window.
To do that I am going to go to my Toolbox, select Button and drag it over to the designer surface. Notice how it was added in the designer and also in my XAML. Now I am going to double-click on the Button and write my code, let's say var p = new printer. You remember a few minutes ago I created a instance method and a static method? So the instance method has to be called by creating an instance of the type, like this. Once I have created the instance then I can say var result = p.
Now when I hit the dot operator I am going to get a listing of all the members of my Printer Class. The one I am interested in this is one right here, GetPrinterName. Now I'll have the printer name stored in this result variables and I am going to pass that into my static method. Now static members are accessed via the name of the type, I don't have to use the new keyword first. So here I am going to say var isReady = Printer.IsPrinterReady, that's my static method. And then I am going to pass in the result from the previous method call.
The last thing I need to do is show the results to you. I can do that one of two ways; I can create some UI on my window or I can use the modal dialog called the MessageBox. I am going to choose the latter, MessageBox.Show. C# supports something called String Formatters, that means that if I put a string I can place tokens within that string and then replaces at runtime. What I will do here is a String.Format, like that, and then the token is a curly brace, close curly brace and then a numeric value. And then after the string I will say Comma (,) isReady.
What's going to happen at runtime is the results that's in here is going to be placed in this token area inside the string. I am ready to run the application. It works the same way as a Silverlight application, you do a build, and then you need to start the application, I will choose start with Debugging. Now you can see my UI, this is a real window, it's part of the Windows operating system, do all the usual Window things, I am going to click on this button to see the results. C# is a nice modern language that is flexible enough to create a rainbow of application types.
Look for more details about how it meshes with Silverlight in the rest of the course.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101634 Viewers
61 Video lessons · 88389 Viewers
71 Video lessons · 72227 Viewers
56 Video lessons · 103975 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.