您的位置:首页 > 移动开发

WPF Application Shutdown Mode

2009-11-23 12:59 162 查看
有三种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
}

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>

配置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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: