#91 – What InitializeComponent() Does

The entry point into a WPF application, the Main function, is quite simple.  It creates an instance of your Application object, calls its InitializeComponent method and then its Run method.

The see the body of the Main function, as well as the body of InitializeComponent, do the following:

  • Build your application
  • Click on the Show All Files icon in the Solution Explorer

  • In Solution Explorer, navigate to App.g.cs file

Double-click to open App.g.cs.  You’ll see the body of Main.  You’ll also see the body of InitializeComponent.

public void InitializeComponent() {

    #line 4 "..\..\..\App.xaml"
    this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);

    #line default
    #line hidden
 }

This function merely sets properties on the Application object based on values that you set in App.xaml.  By default, this includes only setting the StartupUri property to point to the application’s main window.

Update, 20-Oct-2010: InitializeComponent() does a bit more when generated for a Window, rather than for an App. See What Visual Studio Does with Your XAML for details.

Advertisement