在关闭C#WPF应用程序之前询问用户 [英] Ask user before closing C# WPF application

查看:209
本文介绍了在关闭C#WPF应用程序之前询问用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它有效,但只有当用户按X。当用户按下Application.Current.Shutdown();它不起作用。

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(),则无法取消关闭事件。只需调用 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应用程序之前询问用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆