WPF Applications With a Main Method


Click here to change the theme.

WPF_With_Main.zip

This article shows how to create a Windows Presentation Foundation (WPF) application from scratch (an empty project) and do it with a program that has a Main function. The project we will start with is not a WPF or console project. 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 explaing 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.

Nww Project window

There will already be a reference for System. Add the following references:

  • 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 Program class

Add a "using" statement for "System.Windows", then change the Program class to the following:

class Program
{
[STAThread]
static void Main(string[] args)
{
Application a = new Application();
a.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative);
a.Run();
}
}

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:

Add MainWindow

Put the following in MainWindow.txt:

<Window x:Class="WPF_With_Main.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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" Click="Button1_Click"/>
</StackPanel>
</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":

Warning

Build and run the application. You will get a console window plus the following window:

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:

Project Properties

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:

Add cs file

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 will do that.

Use the following code in the "MainWindow.xaml.cs" file:

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 Event

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.