#43 – What Happens in WPF Main() Function

When creating a new WPF standalone application using the New Project wizard in Visual Studio 2013, Visual Studio automatically creates a class that derives from System.Windows.Application, as well as a static Main() function that will be the first function called when the application is started.

Here’s how the main application object is defined:

 public partial class App : Application

During the Main method, the application is started up by:

  • Creating a new instance of the Application object
  • Calling Application.InitializeComponent to constitute the application from App.xaml
  • Calling Application.Run to start the application
public static void Main()
{
    WpfApplication.App app = new WpfApplication.App();
    app.InitializeComponent();
    app.Run();
}

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

4 Responses to #43 – What Happens in WPF Main() Function

  1. johnyao123 says:

    It seems there is no Main() in Visual Studio 2015. What happened?

    • johnyao123 says:

      Is that because I have StartupUri=”MainWindow.xaml” in App.xaml, so Main() is not needed any more?

      • Sean says:

        Main() is still used. It’s the entry point of the application and (by default) invokes the Run method on the main Application object.

    • Sean says:

      Main() is still there and used as the entry point for the application. It’s located in App.g.cs, which is code that is generated from App.xaml at build time. You can see the file by turning on Show All Files option and looking under \obj.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: