在关闭C#WPF应用程序之前询问用户

编程入门 行业动态 更新时间:2024-10-08 08:25:00
本文介绍了在关闭C#WPF应用程序之前询问用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在关闭应用程序之前询问用户.我正在使用C#.NET 4.0 WPF.我可以在Windows窗体中执行此操作,但不能在WPF中执行此操作.当用户想要关闭应用程序时,会触发事件.出现消息框,无论按下哪个按钮(是或否),应用程序始终关闭.为什么?错误在哪里?

I want to ask the user before closing the application. I'm using C# .NET 4.0 WPF. I can do it in windows forms, but not in WPF. Event is fired when the user want to close the app. Message box appears, bun no matter which button is pressed (Yes or No) the application always closes. Why? Where is the mistake?

它起作用,但是仅当用户按下"X"时才起作用.当用户使用 Application.Current.Shutdown(); 按下关闭按钮时,此按钮将不起作用.

It works, but only when the user presses the "X". When the user presses the close button with Application.Current.Shutdown(); it is not working.

private void MainWindowDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e) { MessageBoxResult result = MessageBox.Show("Do you really want to do that?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.No) { e.Cancel = true; } }

推荐答案

如果调用 Application.Current.Shutdown(),则不能取消Closing事件.只需调用 Window.Close()方法即可,这将使您有机会否决关闭操作.一旦您所有程序的窗口都关闭,应用程序将自动关闭.

The Closing event cannot be cancelled if you call Application.Current.Shutdown(). Just call the Window.Close() method instead, which will give you a chance to veto the close operation. Once all your program's windows have closed the application will shutdown automatically.

有关更多信息,请查看MSDN上的应用程序管理页

For more information checkout the Application Management page on MSDN.

更多推荐

在关闭C#WPF应用程序之前询问用户

本文发布于:2023-11-03 05:00:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   用户   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!