This article shows how to create a Windows Presentation Foundation (WPF) application from scratch (an empty project). Unlike applications generated by Visual Studio, this application has a Main method. This is for educational purposes only, I do not know if there is any practical use of this code.
WPF applications are strange, at least the way that Visual Studio does them. When we use Visual Studio to create a WPF application for us, there is no Main function. This article is written for those of us that are experienced with other ways to write Windows desktop aplications, such as Windows Forms applicaitons, console applications and unmanaged C++ Windows API functions.
I hope this article helps you to understand.
- How WPF aplications are executed using the Application class
- How a C# file is specified as the code behind file for a XAML file
This article requires a basic understanding of the use of Visual Studio. A beginner might have difficulty adding references to the project; I am not explaining the details of how to do that.
In Visual Studio click on "File" -> "New" -> "Project...". Create a Visual C# Windows Desktop Empty Project. Use the name "WPF_With_Main"; if you use a different name then you will need to make corresponding changes in the code. The "Add New Project" window will look as in the following.
Add the following six references.
Next add a class called "Program". From the Project menu select "Add New Item...". Choose "Class" from the middle. Change the name to "Program" as in the following.
Add a "using" statement for "System.Windows", then change the Program class to the following.
- class Program
- static void Main(string args)
- Application a = new Application();
- a.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative);
This code will create an instance of the WPF Application class then create an instance of the Uri class specifying MainWindow.xaml as the URI. It then runs the application which will show the XAML.
We will now create the "MainWindow.xaml" file. Visual Studio however will not create just a XAML file by itself. So we will add a text file and then rename it. From the Project menu select "Add New Item...". In the "General" node select "Text File" and change the name to "MainWindow" as in the following.
Put the following in MainWindow.txt.
- <Window x:Class="WPF_With_Main.MainWindow"
- Title="A hello window" Height="300" Width="400">
- <StackPanel HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top">
- <TextBlock TextWrapping="Wrap" Text="Hello"/>
- <Button x:Name="Button1" Content="Click me" Width="75"/>
Then in Solution Explorer right-click on the MainWindow.txt file and from the context menu select "Rename". Change "txt" to "xaml". You will get a warning as in the following; just say "Yes".
Build and run the application. You will get a console window plus the following window.
We do not want the console window! So go to the project's properties and change the application's Output Type to "Windows Application" as in the following.
Now when you run the aplication you will get no console window.
You also will get nothing when you click on the button. So we will now add a code-behind file for the MainWindow. From the Project menu select "Add New Item...". In the "General" node select "Code File" and change the name to "MainWindow.xaml.cs" as in the following.
It is possible to associate a ".cs" file with a ".xaml" file without using the naming convention of suffixing the ".xaml" file name with ".cs" but it is simpler to use the naming convention and we are doing that.
Use the following code in the "MainWindow.xaml.cs" file.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace WPF_With_Main
- partial class MainWindow : Window
If the window for the "MainWindow.xaml" file is still open then close it and re-open it now. This will allow the designer to open it as a XAML file. Then go to the properties for the button in the XAML file and switch to the events. Double-click on the Click event to add a Click event for the button as in the following.
Add the following to the Button1_Click method.
- MessageBox.Show("Thank you");
Build and run the application. You can now see that you can create an instance of the WPF Application class then set the StartupUri property to a XAML file then run the application and the XAML file will be the main window. If you look at a WPF application generated by Visual Studio then you will see that it uses the StartupUri property in the "App.xaml" file.