WPF应用程序生存期以及相关事件

  •  WPF 应用程序的生存期会通过 Application 引发的几个事件来加以标记,相关事件对应着应用程序何时启动、激活、停用和关闭。

应用程序生存期事件

  • • 独立应用程序(传统风格的 Windows 应用程序,这些应用程序作为要安装到客户端计算机并从客户端计算机运行的可执行程序集来生成)和 XAML 浏览器应用程序 (XBAP)(由导航页组成的应用程序,这些应用程序作为可执行程序集生成并由Web 浏览器托管) 的生存期并非完全相同。

  • • 下图展示了独立应用程序生存期内的各个关键事件及其引发顺序。

 

 同样的,下图展示了 XAML 浏览器应用程序 (XBAP)生存期内的各个关键事件及其引发顺序。

 

创建WPF应用程序并验证生存期相关事件

  • • 在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);
            });
        }
    }
}

 

相关推荐

  1. WPF.NET开发】对象生存期事件

    2024-01-20 14:08:03       39 阅读
  2. WPF 键盘事件捕获

    2024-01-20 14:08:03       26 阅读
  3. WPF 应用程序中实现单例模式

    2024-01-20 14:08:03       38 阅读
  4. WPF.NET】演练:创建触控应用程序

    2024-01-20 14:08:03       37 阅读
  5. WPF.NET开发】将字体与应用程序一起打包

    2024-01-20 14:08:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 14:08:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 14:08:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 14:08:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 14:08:03       18 阅读

热门阅读

  1. 【issue-halcon例程学习】ball.hdev

    2024-01-20 14:08:03       29 阅读
  2. 动态sql,关联查询

    2024-01-20 14:08:03       32 阅读
  3. Webpack打包vue项目

    2024-01-20 14:08:03       36 阅读
  4. 173. 二叉搜索树迭代器

    2024-01-20 14:08:03       33 阅读
  5. x86 和 x64 arm的区别

    2024-01-20 14:08:03       34 阅读
  6. JUnit 5 单元测试框架

    2024-01-20 14:08:03       35 阅读