1、app.xaml中设置
要在App.xaml中添加 SessionEnding的响应事件。代码如下所示
<Application x:Class="YC3000.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup" ShutdownMode="OnExplicitShutdown" SessionEnding="App_SessionEnding">
2、在App.xaml.cs中添加响应事件。
//系统关机拦截
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
// Ask the user if they want to allow the session to end
string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);
MessageBoxResult result = MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);
// End session, if specified
if (result == MessageBoxResult.No)
{
e.Cancel = true;
}
}
3、如果拦截关机事件没有影响应该是开启了快速关机引起的。
修改注册表,HKEY_CURRENT_USER\Control Panel\Desktop中AutoEndTasks应该设置为0或者删除,来阻止快速关机。