zoukankan      html  css  js  c++  java
  • WPF实现在电脑重启或关机时执行某些逻辑

    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;

  • 相关阅读:
    javascript运算符
    javascript字符串转数字
    javascript的变量声明和数据类型
    javascript的历史和入门
    CSS中定位
    CSS中盒子模型
    CSS操作表格的边框和表格的属性示例代码
    常用的CSS样式示例代码
    CSS伪类选择器
    CSS选择器
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/6641008.html
Copyright © 2011-2022 走看看