SessionEnding
By default, an application shuts down when the Windows session ends, which occurs when a user logs off or shuts down.
You can detect when a session ends by handling the SessionEnding event. If an application needs to prevent the session from ending, the SessionEndingCancelEventArgs argument that is passed to the event handler exposes the Cancel that you set to true (the default value is false).
注意SessionEnding的适用范围:
SessionEnding is not raised by console applications or XAML browser applications (XBAPs) .
SessionEnding is raised only on the thread that creates the Application object.
例子:
在退出windows session的时候弹出对话框让用户选择是否退出。
{
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;
}
}
}
然后在XAML里配置,让上面的方法与SessionEnding Event相关联。
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml%22
StartupUri="Window1.xaml"
SessionEnding="App_SessionEnding">
</Application>
DispatcherUnhandledException
Occurs when an unhandled exception occurs anywhere in your application (on the main application thread).
By responding to this event, you can log critical errors.
You can even choose to neutralize the exception and continue running your application by setting the
DispatcherUnhandledExceptionEventArgs.Handled property to true.
指定event handler的方法还是一样,先写好方法:
{
MessageBox.Show("An unhandled " + e.Exception.GetType().ToString() +
" exception was caught and ignored.");
e.Handled = true; // 把该异常设置为已处理,以防止程序因此退出
}
再编辑XAML:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException"
>
</Application>
用override的方式来指定event handler
重载哪个方法?很简单,event叫什么名字,在前面加个On就是了。例如OnStartup,OnSessionEnding。只有DispatcherExceptionUnhandled事件是个例外,没有OnDispatcherExceptionUnhandled方法,只有像上文那样在XAML中明确写出event handler了。
{
private bool unsavedData = false;
public bool UnsavedData
{
get { return unsavedData; }
set { unsavedData = value; }
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); // call base first
UnsavedData = true;
}
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e); // call base first
if (UnsavedData)
{
e.Cancel = true;
MessageBox.Show(
"The application attempted to be closed as a result of " +
e.ReasonSessionEnding.ToString() +
". This is not allowed, as you have unsaved data.");
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架