No Main() in WPF?

Juice picture Juice · Apr 22, 2010 · Viewed 71.7k times · Source

I am a beginner when it comes to programming but I was sure that one of the universal rules was that a program starts with Main(). I do not see one when I create a WPF project. Is Main() simply named something differently in WPF?

Answer

Andreas Kahler picture Andreas Kahler · Nov 12, 2014

The Main() method is created automatically. If you want to provide your own you have to (tested in VS2013 and VS2017):

  • Right-click App.xaml in the solution explorer, select Properties
  • Change 'Build Action' to 'Page' (initial value is 'ApplicationDefinition')

Then just add a Main() method to App.xaml.cs. It could be like this:

[STAThread]
public static void Main()
{
    var application = new App();
    application.InitializeComponent();
    application.Run();
}