-
?WPF?應(yīng)用程序的生存期會(huì)通過(guò) Application 引發(fā)的幾個(gè)事件來(lái)加以標(biāo)記,相關(guān)事件對(duì)應(yīng)著應(yīng)用程序何時(shí)啟動(dòng)、激活、停用和關(guān)閉。
應(yīng)用程序生存期事件
-
??獨(dú)立應(yīng)用程序(傳統(tǒng)風(fēng)格的 Windows 應(yīng)用程序,這些應(yīng)用程序作為要安裝到客戶端計(jì)算機(jī)并從客戶端計(jì)算機(jī)運(yùn)行的可執(zhí)行程序集來(lái)生成)和 XAML 瀏覽器應(yīng)用程序 (XBAP)(由導(dǎo)航頁(yè)組成的應(yīng)用程序,這些應(yīng)用程序作為可執(zhí)行程序集生成并由Web 瀏覽器托管) 的生存期并非完全相同。
-
??下圖展示了獨(dú)立應(yīng)用程序生存期內(nèi)的各個(gè)關(guān)鍵事件及其引發(fā)順序。
?
?同樣的,下圖展示了 XAML 瀏覽器應(yīng)用程序 (XBAP)生存期內(nèi)的各個(gè)關(guān)鍵事件及其引發(fā)順序。
?
創(chuàng)建WPF應(yīng)用程序并驗(yàn)證生存期相關(guān)事件
-
??在app.xmal中綁定相關(guān)事件以及啟動(dòng)設(shè)置Shutdown返回代碼模式
?
// app.xanl 文件
// 綁定相關(guān)事件
<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 相關(guān)代碼
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>
/// 啟動(dòng)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Startup(object sender, StartupEventArgs e)
{
Debug.WriteLine("應(yīng)用啟動(dòng)了:Startup");
//this.ShutdownMode = ShutdownMode.OnMainWindowClose;
}
/// <summary>
/// 獲得焦點(diǎn)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Activated(object sender, EventArgs e)
{
Debug.WriteLine("應(yīng)用獲得焦點(diǎn)了:Activated");
}
/// <summary>
/// 失去焦點(diǎn)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Deactivated(object sender, EventArgs e)
{
Debug.WriteLine("應(yīng)用失去焦點(diǎn)了:Deactivated");
}
/// <summary>
/// 異常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Debug.WriteLine("應(yīng)用出現(xiàn)未處理的異常:DispatcherUnhandledException");
//設(shè)置為已處理了異常
e.Handled = true;
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Exit(object sender, ExitEventArgs e)
{
Debug.WriteLine("程序關(guān)閉退出了:Exit");
}
}
}
//MainWindow.xaml.cs 相關(guān)代碼
// 按鈕里模擬相關(guān)處理
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);
});
}
}
}
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-808635.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-808635.html
到了這里,關(guān)于WPF應(yīng)用程序生存期以及相關(guān)事件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!