WPF Applications with a Main Method

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.

  • System
  • System.Windows
  • System.Xaml
  • WindowsBase
  • PresentationCore
  • PresentationFramework

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.

  1. class Program  
  2. {  
  3.     [STAThread]  
  4.     static void Main(string[] args)  
  5.     {  
  6.         Application a = new Application();  
  7.         a.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative);  
  8.         a.Run();  
  9.     }  
  10. }  

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.

  1. <Window x:Class="WPF_With_Main.MainWindow"  
  2.   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.   Title="A hello window" Height="300" Width="400">  
  5.     <StackPanel HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top">  
  6.         <TextBlock TextWrapping="Wrap" Text="Hello"/>  
  7.         <Button x:Name="Button1" Content="Click me" Width="75"/>  
  8.     </StackPanel>  
  9. </Window>  

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  8. namespace WPF_With_Main
  9. {  
  10.     partial class MainWindow : Window  
  11.     {  
  12.     }  
  13. }  

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.

  1. 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.