Application类的SessionEnding事件,就是电脑关机或重启时响应的(会话结束事件),

所以只需要在App.xaml中添加SessionEnding

<Application x:Class="DriverEasyWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup" SessionEnding="Application_SessionEnding">

并在后台代码中这样实现:

private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
//your code
}

但Windows没有SessionEnding事件,那如果要在某个窗口中监听电脑关机或重启呢,

因为Application类是贯穿于整个WPF项目的,所以在其他窗口的后台代码中,这样调用即可:

public MainWindow()
{
InitializeComponent();
App.Current.SessionEnding += Current_SessionEnding;
} private void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
throw new NotImplementedException();
}

通过 App.Current.SessionEnding += Current_SessionEnding;

最新文章

  1. VR ( Virtual Reality )、AR(Augmented Reality)、MR(Mix Reality)和CR(Cinematic Reality)是什么鬼?
  2. 将 instance 连接到 second_local_net - 每天5分钟玩转 OpenStack(85)
  3. eclipse maven update error 解决方法
  4. web页面的加载顺序
  5. pecl install imagick
  6. css构造文本
  7. android studio开发工具的android library打包文件(.aar)本地引用
  8. Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -&gt; [Help 1]
  9. ###学习《C++ Primer》- 2
  10. myeclipse-8.6.0下载
  11. 获取被选择的radio的值
  12. SQL SERVER 2005 同步复制技术(转)
  13. Mac新手入门使用教程 - Finder 技巧
  14. Netsharp系列文章目录结构
  15. selenium +chromdriver模块
  16. 如何检查oracle的归档日志及空间占用率,清除归档日志
  17. python对redis的常用操作 上 (对列表、字符串、散列结构操作)
  18. msp430学习笔记-DAC12
  19. java正则表达式简介
  20. 爬虫实战【10】利用Selenium自动登陆京东签到领金币

热门文章

  1. STM32F10X-定时器/计数器
  2. day37(类加载器)
  3. 《mysql必知必会》学习_第12章_20180801_欢
  4. POJ1644状态转移的思想——排列组合
  5. [php-array] PHP 数组的怪异之处
  6. python使用httplib2访问REST服务的例子
  7. Windows核心编程:第12章 纤程
  8. 从NetCore报错到MySql安全
  9. 文本框仅可接收decimal
  10. 【转】JS中的call()和apply()方法