简体   繁体   English

在 WPF 中关闭事件后保持应用程序运行

[英]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.如果您想阻止关闭,您必须将CancelEventArgsCancel属性设置为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:两件事情:

  • To cancel the shutdown, set e.Cancel = true;要取消关闭,请设置e.Cancel = true; and that's it.就是这样。
  • If you're OK for it to shutdown, just let the code exit the method without setting the Cancel property.如果您可以关闭它,只需让代码退出该方法而不设置 Cancel 属性。 No need to call Shutdown();无需调用 Shutdown();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在关闭时使WPF应用程序保持活动状态 - Keep WPF application alive on Closing 关闭Windows XP后,C#应用程序继续运行 - C# application keep running after closing windows XP 在单击事件后加载时,功能区应用程序菜单(下拉菜单)未在 WPF 应用程序中最小化(关闭或失去焦点) - Ribbon application menu (dropdown) is not minimizing (closing or losing focus) in WPF application while loading after a click event 关闭程序后如何保持进程运行? - How to keep process running after closing the program? WPF C#应用程序在关闭后保持运行几秒钟。 这是安全的解决方案吗? - WPF C# application keeps running for a few seconds after closing. Is this a safe solution? 在WPF应用程序运行时保持Windows清醒 - Keep Windows awake when a WPF application is running WPF在Closing事件中隐藏窗口阻止应用程序终止 - WPF hide a window in Closing event prevents from application terminating WPF应用程序的关闭是否引起Window.Closing事件? - Is shutdown of WPF application cause Window.Closing event? 想要在关闭Web浏览器后继续运行交换迁移代码 - Want to keep running the exchange migration code after closing the web browser Application.Exit()-关闭表单后应用程序仍在运行 - Application.Exit() - application still running after closing form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM