Application MainWindow in WPF

MainWindow property gets or sets the main window of the application. The code snippet in Listing 4 gets the MainWindow.

Application curApp = Application.Current;
Window mainWnd = curApp.MainWindow;

Listing 4

The code snippet in Listing 5 sets MainWindow property in XAML.

<Application x:Class="WPFSample.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="Window1.xaml"

             Startup="App_Startup">

    <Application.MainWindow>

        <NavigationWindow Source="Window1.xaml" Visibility="Visible">
        </
NavigationWindow>

    </Application.MainWindow>
</
Application>

Listing 5