首页 > Windows开发 > 详细

WPF实现在电脑重启或关机时执行某些逻辑

时间:2017-03-29 19:06:34      阅读:440      评论:0      收藏:0      [点我收藏+]

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">
View Code

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

技术分享
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
             //your code
        }
View Code

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

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

技术分享
public MainWindow()
        {
            InitializeComponent(); 
            App.Current.SessionEnding += Current_SessionEnding;
        }

      

        private void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            throw new NotImplementedException();
        }
View Code

   通过 App.Current.SessionEnding += Current_SessionEnding;

WPF实现在电脑重启或关机时执行某些逻辑

原文:http://www.cnblogs.com/tommy-huang/p/6641008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!