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

Put the following in MainWindow.txt.





Hashim ShafiqPosted Aug 21, 2015, 10:13 PM
Nice
Karthik Muthu KaruppanPosted May 4, 2015, 2:02 PM
nice
Akhil GargPosted May 3, 2015, 5:43 AM
nice article. ..:)
Santhakumar MunuswamyPosted May 3, 2015, 1:37 AM
Thanks for nice article sir