自定义WPF MessageBox不是';t返回结果

本文关键字:返回 结果 WPF MessageBox 不是 自定义 | 更新日期: 2023-09-27 18:27:29

我已经学习了关于使用WPF创建自定义MessageBoxes的教程(此处)。不过我有一个小问题,因为目前我的自定义框没有返回MessageBoxResult。我不明白为什么会这样。

这是xaml文件PDSAMessageBoxView:

 <Window x:Class="Resources.PDSAMessageBoxView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="NoResize"
    ShowInTaskbar="True"
    FontFamily="Segoe UI"
    WindowStartupLocation="CenterScreen"
    Height="300"
    Width="420"
     >
 <Border>
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
     <TextBlock Name="tbMessage"
     Text="Message goes here..."
     TextWrapping="Wrap" />
        <StackPanel Grid.Row="1"
                    >
            <Button Content="Yes"
                     x:Name="btnYes"
                     Click="btnYes_Click" />
            <Button Content="No"
                     x:Name="btnNo"
                     Click="btnNo_Click" />
            <Button Content="OK"
                     x:Name="btnOk"
                     Click="btnOk_Click" />
            <Button Content="Cancel"
                     x:Name="btnCancel"
                     Click="btnCancel_Click" />
        </StackPanel>
    </Grid>
</Border>
</Window>

这是相应的PDSAMessageBoxView.xaml.cs文件:

public partial class PDSAMessageBoxView : Window
{
    public PDSAMessageBoxView()
    {
        InitializeComponent();
    }
    public static MessageBoxResult Show(string message)
    {
        return Show(message, string.Empty, MessageBoxButton.OK);
    }
    public static MessageBoxResult Show(string message,
      string caption)
    {
        return Show(message, caption, MessageBoxButton.OK);
    }
    public static MessageBoxResult Show(string message,
      string caption, MessageBoxButton buttons)
    {
        MessageBoxResult result = MessageBoxResult.None;
        PDSAMessageBoxView dialog = new PDSAMessageBoxView();
        dialog.Title = caption;
        dialog.tbMessage.Text = message;
        dialog.Buttons = buttons;
        // If just an OK button, allow the user to just 
        // move away from the dialog
        if (buttons == MessageBoxButton.OK)
            dialog.Show();
        else
        {
            dialog.ShowDialog();
            result = dialog.Result;
        }
        return result;
    }
    

    public MessageBoxButton Buttons { get; set; }
    public MessageBoxResult Result { get; set; }
    private void btnYes_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnNo_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
    }
}

最后,代码被称为:

MessageBoxResult result = Resources.PDSAMessageBoxView.Show("Are you sure you want to exit?", "Confirm Shutdown",
            System.Windows.MessageBoxButton.YesNo);

返回的结果总是None。而且显示的按钮总是yesnocancelok,而它们应该只有YesNo

如果有人能为我指明正确的方向,我将不胜感激。

自定义WPF MessageBox不是';t返回结果

收件人:

返回的结果始终为None。而且显示的按钮总是yes、no、cancel和ok,而它们应该只有yes和no。

你只是忘记了实现这个功能。当此(public MessageBoxButton Buttons { get; set; })属性更改时,必须隐藏和显示按钮。

下一件事是,你忘记了在你的按钮点击方法中设置结果::

 private void btnYes_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnNo_Click(object sender, RoutedEventArgs e)
    {
    }
    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
    }

你还没有完成每一件事,仅此而已。

这里有一个正确的实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MessageBoxHelp
{
    public partial class PDSAMessageBoxView : Window
    {
        private MessageBoxButton buttons;
        public PDSAMessageBoxView()
        {
            InitializeComponent();
        }
        public static MessageBoxResult Show(string message)
        {
            return Show(message, string.Empty, MessageBoxButton.OK);
        }
        public static MessageBoxResult Show(string message,
          string caption)
        {
            return Show(message, caption, MessageBoxButton.OK);
        }
        public static MessageBoxResult Show(string message,
          string caption, MessageBoxButton buttons)
        {
            MessageBoxResult result = MessageBoxResult.None;
            PDSAMessageBoxView dialog = new PDSAMessageBoxView();
            dialog.Title = caption;
            dialog.tbMessage.Text = message;
            dialog.Buttons = buttons;
            // If just an OK button, allow the user to just 
            // move away from the dialog
            if (buttons == MessageBoxButton.OK)
                dialog.Show();
            else
            {
                dialog.ShowDialog();
                result = dialog.Result;
            }
            return result;
        }


        public MessageBoxButton Buttons
        {
            get
            {
                return buttons;
            }
            set
            {
                buttons = value;
                btnCancel.Visibility = Visibility.Collapsed;
                btnOk.Visibility = Visibility.Collapsed;
                btnYes.Visibility = Visibility.Collapsed;
                btnNo.Visibility = Visibility.Collapsed;
                switch (buttons)
                {
                    case MessageBoxButton.OK:
                        btnOk.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.OKCancel:
                        btnCancel.Visibility = Visibility.Visible;
                        btnOk.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNo:
                        btnYes.Visibility = Visibility.Visible;
                        btnNo.Visibility = Visibility.Visible;
                        break;
                    case MessageBoxButton.YesNoCancel:
                        btnCancel.Visibility = Visibility.Visible;
                        break;
                }
            }
        }
        public MessageBoxResult Result { get; set; }
        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.Yes;
            this.Close();
        }
        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.No;
            this.Close();
        }
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.OK;
            this.Close();
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.Cancel;
            this.Close();
        }
    }
}

我已经将你的命名空间更改为MessageBoxHelp,因为在我的情况下,资源是一个糟糕的选择。