有三种shutdown mode
Member name |
Description |
OnLastWindowClose |
An application shuts down when either the last window closes, or Shutdown is called. |
OnMainWindowClose |
An application shuts down when either the main window closes, or Shutdown is called. |
OnExplicitShutdown |
An application shuts down only when Shutdown is called. |
其中OnMainWindowClose这个挺有用,但首先要知道如何指定MainWindow。
指定MainWindow的两种途径:
1. 在代码中指定
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public App()
{
Window oldMainWindow = this.MainWindow;// Get main window
Window newMainWindow = new Window1();
this.MainWindow = new Window();// Set mainwindow
}
/// Interaction logic for App.xaml
/// </summary>
public App()
{
Window oldMainWindow = this.MainWindow;// Get main window
Window newMainWindow = new Window1();
this.MainWindow = new Window();// Set mainwindow
}
2. 在XAML中指定
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="StartupWindow.xaml"
>
<Application.MainWindow>
<NavigationWindow Source="MainPage.xaml" Visibility="Visible"></NavigationWindow>
</Application.MainWindow>
</Application>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="StartupWindow.xaml"
>
<Application.MainWindow>
<NavigationWindow Source="MainPage.xaml" Visibility="Visible"></NavigationWindow>
</Application.MainWindow>
</Application>
配置Shutdown Mode
这里我们选择OnMainWindowClose:
<Application x:Class="TestApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml%22
StartupUri="Window1.xaml"
ShutdownMode="OnMainWindowClose"
>
</Application>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml%22
StartupUri="Window1.xaml"
ShutdownMode="OnMainWindowClose"
>
</Application>