如何在wpf中的窗口关闭按钮上显示消息?

Nad*_*eem 0 .net wpf

当用户单击您要关闭的关闭按钮时,我想显示一条消息?

Jef*_*ado 5

只需为Closing事件添加一个处理程序并显示一个包含您的消息的消息框。根据用户选择的选项取消事件。

在 C# 中:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
    }

    void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (MessageBox.Show("ARE YOU WANT TO CLOSE?", "CLOSING", MessageBoxButton.YesNo) == MessageBoxResult.No)
        {
            e.Cancel = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)