简体   繁体   English

当用户单击 WPF 中的 X 按钮时,如何显示 MessageBox 提示

[英]How do I show a MessageBox promp when the user has clicked the X button in WPF

I am working on a C# WPF application.我正在处理 C# WPF 应用程序。 After i get some input from the user i need to check some conditions and if the condition don't match and the user has pressed the X button I need to be able to show a MessageBox containing an error.在我从用户那里得到一些输入后,我需要检查一些条件,如果条件不匹配并且用户按下了 X 按钮,我需要能够显示一个包含错误的 MessageBox。 After the user has pressed ok in the MessageBox i need the user to return to the previous window. My code looks something like this.用户在 MessageBox 中按下确定后,我需要用户返回到之前的 window。我的代码看起来像这样。

  private void Window_Closing(object sender, CancelEventArgs e)
  {
     MessageBoxResult closingMessegeBoxResult = MessageBox.Show("Is it OK to close?", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     if (closingMessegeBoxResult != MessageBoxResult.OK)
     {
        e.Cancel = true;
        this.Activate();
     }
  }
     private void Window_Closed(object sender, EventArgs a)
  {

  }

For now i only want to be able to show the MessageBox with a random error.现在我只想能够显示带有随机错误的 MessageBox。

Your code is working properly.您的代码工作正常。

You have to add OnClosing event to the window,您必须将 OnClosing 事件添加到 window,

And when you click on close of the window you will see the message当您单击 window 的关闭时,您将看到消息

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestProject"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800"

    Closing="MainWindow_OnClosing">
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MainWindow_OnClosing(object? sender, CancelEventArgs e)
    {
        MessageBoxResult closingMessegeBoxResult = MessageBox.Show("Is it OK to close?", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        if (closingMessegeBoxResult == MessageBoxResult.OK)
        {
            e.Cancel = true;
            this.Activate();
        }
    }
}

You should use the MessageBoxButton.OKCancel option to enable the user to avoid closing the window:您应该使用MessageBoxButton.OKCancel选项来使用户避免关闭 window:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    MessageBoxResult closingMessegeBoxResult = MessageBox.Show("Is it OK to close?", "Error",
        MessageBoxButton.OKCancel, MessageBoxImage.Error);
    if (closingMessegeBoxResult != MessageBoxResult.OK)
    {
        e.Cancel = true;
    }
}

Using the above code, the window will only be closed when the user clicks the "OK" button of the dialog.使用上面的代码,window 只会在用户单击对话框的“确定”按钮时关闭。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM