在关闭应用程序时显示通知(例如消息框)
本文关键字:消息 通知 应用程序 显示 | 更新日期: 2023-09-27 18:04:36
在我的应用程序中,我以如下所示的方式重写OnClose
事件。由于应用程序可能需要一些时间来执行SynchronizeLotsaStuff
,所以我想通知用户应用程序即将关闭。
我尝试使用MessageBox,但它阻止了程序的继续,并且还显示了一个不需要的"OK"按钮。
我想我更喜欢一些更有特色的东西,比如褪色/灰色窗口,甚至是一个"闪屏"来关闭,但一个常规的消息框也会很好。
// MainWindow.xaml.cs:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Wait while application is closed...");
if (this.DataContext != null) {
var vm = this.DataContext as ShellViewModel;
// possibly long-running method
vm.SynchronizeLotsaStuff();
}
base.OnClosing(e);
}
更新:按照Stijn Bernards的建议,我把MessageBox的东西在一个线程,但我还没有发现(是的,我谷歌)一个适当的方式来终止它。即使是Abort
,在主窗口关闭后,消息框仍然显示,直到我点击"确定"按钮。
// MainWindow.xaml.cs:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
var messagethread = new Thread(new ThreadStart(() => {
MessageBox.Show("Aguarde enquanto o aplicativo é encerrado...");
}));
messagethread.Start();
if (this.DataContext != null) {
var vm = this.DataContext as ShellViewModel;
// possibly long-running method
vm.SynchronizeLotsaStuff();
}
// UGLY UGLY UGLY (and doesn't work either)
messagethread.Abort();
base.OnClosing(e);
}
我试过使用abort线程,令我惊讶的是它不工作。
我可以说这看起来不容易…
所以我建议看看这个:点击我
这是一个关于如何创建自己的消息框的很好的教程,它还可以帮助你解决删除ok按钮的问题。
欢呼。