?WPF?应用程序的生存期会通过 Application 引发的几个事件来加以标记,相关事件对应着应用程序何时启动、激活、停用和关闭。
??独立应用程序(传统风格的 Windows 应用程序,这些应用程序作为要安装到客户端计算机并从客户端计算机运行的可执行程序集来生成)和 XAML 浏览器应用程序 (XBAP)(由导航页组成的应用程序,这些应用程序作为可执行程序集生成并由Web 浏览器托管) 的生存期并非完全相同。
??下图展示了独立应用程序生存期内的各个关键事件及其引发顺序。
?
?同样的,下图展示了 XAML 浏览器应用程序 (XBAP)生存期内的各个关键事件及其引发顺序。
?
??在app.xmal中绑定相关事件以及启动设置Shutdown返回代码模式
?
// app.xanl 文件
// 绑定相关事件
<Application x:Class="WpfFirstApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfFirstApp"
StartupUri="MainWindow.xaml"
Startup="Application_Startup"
Activated="Application_Activated"
Deactivated="Application_Deactivated"
DispatcherUnhandledException="App_DispatcherUnhandledException"
Exit="Application_Exit"
ShutdownMode="OnMainWindowClose">
<Application.Resources>
</Application.Resources>
</Application>
// App.xaml.cs 相关代码
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
namespace WpfFirstApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Startup(object sender, StartupEventArgs e)
{
Debug.WriteLine("应用启动了:Startup");
//this.ShutdownMode = ShutdownMode.OnMainWindowClose;
}
/// <summary>
/// 获得焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Activated(object sender, EventArgs e)
{
Debug.WriteLine("应用获得焦点了:Activated");
}
/// <summary>
/// 失去焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Deactivated(object sender, EventArgs e)
{
Debug.WriteLine("应用失去焦点了:Deactivated");
}
/// <summary>
/// 异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Debug.WriteLine("应用出现未处理的异常:DispatcherUnhandledException");
//设置为已处理了异常
e.Handled = true;
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Exit(object sender, ExitEventArgs e)
{
Debug.WriteLine("程序关闭退出了:Exit");
}
}
}
//MainWindow.xaml.cs 相关代码
// 按钮里模拟相关处理
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfFirstApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher.Invoke(() =>
{
throw new Exception("模拟异常");
//throw new InvalidOperationException("模拟异常");
});
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Dispatcher.Invoke(() =>
{
Application.Current.Shutdown(9999);
});
}
}
}
?