Redigera

Dela via


How to get or set the main application window (WPF .NET)

This article teaches you how to get or set the main application window for Windows Presentation Foundation (WPF). The first Window that is instantiated within a WPF application is automatically set by Application as the main application window. The main window is referenced with the Application.MainWindow property.

Much of the time a project template will set the Application.StartupUri to a XAML file within your application, such as _Window1.xaml_. This is the first window instantiated and shown by your application, and it becomes the main window.

Tip

The default behavior for an application is to shutdown when the last window is closed. This behavior is controlled by the Application.ShutdownMode property. Instead, you can configure the application to shutdown if the MainWindow is closed. Set Application.ShutdownMode to OnMainWindowClose to enable this behavior.

Set the main window in XAML

The templates that generate your WPF application typically set the Application.StartupUri property to a XAML file. This property is helpful because:

  1. It's easily changeable to a different XAML file in your project.
  2. Automatically instantiates and displays the specified window.
  3. The specified window becomes the Application.MainWindow.
<Application x:Class="MainApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MainApp"
             StartupUri="Window1.xaml">
    
</Application>

Instead of using Application.StartupUri, you can set the Application.MainWindow to a XAML-declared window. However, the window specified here won't be displayed and you must set its visibility.

<Application x:Class="MainApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MainApp">

    <Application.MainWindow>
        <local:Window2 Visibility="Visible" />
    </Application.MainWindow>
</Application>

Caution

If you set both the Application.StartupUri and the Application.MainWindow properties, you'll display both windows when your application starts.

Also, you can use the Application.Startup event to open a window. For more information, see Use the Startup event to open a window.

Set the main window in code

The first window instantiated by your application automatically becomes the main window and is set to the Application.MainWindow property. To set a different main window, change this property to a window:

Application.Current.MainWindow = new Window2();

Application.Current.MainWindow.Show();
Application.Current.MainWindow = New Window2()

Application.Current.MainWindow.Show()

If your application has never created an instance of a window, the following code is functionally equivalent to the previous code:

var appWindow = new Window2();

appWindow.Show();
Dim appWindow As New Window2()

appWindow.Show()

As soon as the window object instance is created, it's assigned to Application.MainWindow.

Get the main window

You can access the window chosen as the main window by inspecting the Application.MainWindow property. The following code displays a message box with the title of the main window when a button is clicked:

private void Button_Click(object sender, RoutedEventArgs e) =>
    MessageBox.Show($"The main window's title is: {Application.Current.MainWindow.Title}");
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    MessageBox.Show($"The main window's title is: {Application.Current.MainWindow.Title}")
End Sub

See also