Creating a WPF application is really a piece of cake if you have Visual Studio with out. All you have to do is,
- Start Visual Studio ( >= 2008)
- Create New Project (project type, WPF Application)
- Hurrah, we have a WPF application now. Go on, edit your Window1.xaml and press F5 to see your WPF application in action
WHERE ?
Simple isn’t it ?? But there are a series of where questions to be asked in here.
- Where is the main function (entry point for the WPF application) ?
- How did Visual Studio found out that Window1 is my top level window (parent window) ?
- From where did it create the instance of Window1 to show it on the screen ?
- *
Lets try to figure out the answers now.
1. Where is the main method ? Answer is fairly simple. Visual Studio has created the main function for the application during the run time. If you notice the solution explorer, there will be two xaml files. App.xaml and Window1.xaml (by default). If you open the properties of App.xaml, one of the settings would be Build Action. This would be set to Application Definition, meaning the WPF application’s start point is this class.
2 and 3. Where is the instance of Window1 ? For this, we need to explore the App.xaml file. As mentioned in the previous inference, App.xaml has been made as the start point thru’ properties, so we need to explore the this file for the startup windows. If you see the code (xaml code),
<Application x:Class="Foo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
the application tag has an attribute called StartupUri whose value is Window1. This means that when the class App is getting instantiated and when the it is run, it should automatically create an instance of Window1.xaml class (as mentioned in the StartupUri attribute) and show it.
But WHY ?
So now that we have found answers for where, we still have a set of why questions to be answered
- Why do we need an App.xaml file, can I run my application without it ?
- Why did Visual Studio create the main method ? Why can’t I do it ?
- Can I show my Window1 myself ? I don’t want Visual Studio to do that
- *
Lets figure out the answers now,
1. Can I run my application without App.xaml file ? No. Its not possible.
2. Why did Visual Studio create the main method ? For this we need to understand how the WPF application works. Every WPF application is nothing but an instance of the System.Windows.Application class. To run a WPF application, first we need to create an instance of this class manually. This should be done inside the main method. As the App.xaml has the entry point, it should create an object of the System.Windows.Application and it needs to call Run in that code behind file (App.xaml.cs).
System.Windows.Application myApp = new System.Windows.Application();
myApp.Run(); //Will run the application
Doing so will run the applicaiton. But still we will not be able to see any windows on the screen.
If you notice, the App.xaml class will be of type Application and not Window unlike the Window1.xaml file. Check out the root tag of App.xaml file.
3. Can I show my Window1 myself ? After creating an instance of System.Windows.Application class, we can create an object of the window class (top level window) in the main method and then call Show on it.
Window1 myWindow = new Window1();
myWindow.Show(); //Will show the window on the screen
Atlast, your main method should look something like this,
class Program
[STAThread] //.net norms for com
static void Main()
{
System.Windows.Application myApp = new System.Windows.Application();
Window1 myWindow = new Window1();
myWindow.Show(); //Will show the window on the screen
myApp.Run(); //Will run the application
}
Visual Studio didn’t want its users to mess with all these things. So most of these are done automatically leaving the users just to bother about their application and its user interfaces. Anyways its nice to know about these when you write a WPF application.
In the next post, I will write about System.Windows.Application class