[英]Keep Application running after Closing Event in WPF
I have declared a closing event for my main application Window.xaml
.我已经为我的主应用程序
Window.xaml
声明了一个关闭事件。 In there, I am asking the user via MessageBox
if he is sure to close the application without saving or not, but no matter the result of the message box, my application shuts down anyway.在那里,我通过
MessageBox
询问用户他是否确定不保存就关闭应用程序,但无论消息框的结果如何,我的应用程序还是会关闭。 What is the problem here?这里有什么问题?
Code of the closing event:关闭事件代码:
private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
var content = ((FrameworkElement)ReportGeneratorFrame.Content);
var dataContext = (ReportGeneratorViewModel)content.DataContext;
string msg = "Schließen ohne Zwischenspeichern?";
MessageBoxResult result =
MessageBox.Show(
msg,
"Speicherabfrage!",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
// If user doesn't want to save, close Application
System.Windows.Application.Current.Shutdown();
}
else
{
dataContext.SaveAllVariables();
}
}
You have to set the Cancel
property in CancelEventArgs
to true
if you want to prevent closing.如果您想阻止关闭,您必须将
CancelEventArgs
的Cancel
属性设置为true
。
if (result == MessageBoxResult.No)
{
e.Cancel = true;
dataContext.SaveAllVariables();
}
The default value for Cancel
is false
, so the application will shutdown anyway when escaping your handler, you do not have to call Shutdown();
Cancel
的默认值为false
,因此应用程序在转义处理程序时无论如何都会关闭,您不必调用Shutdown();
. .
Two things:两件事情:
e.Cancel = true;
e.Cancel = true;
and that's it.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.