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