为什么 DialogResult 在 WPF 中是可为 null 的 bool?

发布于 2024-07-23 08:24:16 字数 98 浏览 8 评论 0原文

有人能想到一个好的解释来解释 WPF 中对话框的结果是一个可为 null 的 bool 值吗? 这一直令我困惑。 在 WinForms 中,它是一个枚举类型,这对我来说更有意义。

Can anyone think of a good explanation for the fact that result of a dialog is a nullable bool in WPF? This has always baffled me. In WinForms it was an enum type and that made a lot more sense to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过期情话 2024-07-30 08:24:16

DialogResult 属性在 Window 类上定义。 并非所有Window都是对话框。 因此,该属性并不与所有窗口相关。 通过 Show() 而不是 ShowDialog() 显示的 Window 将(大概,除非您出于某种原因设置它)具有 <代码>DialogResult = null。

下面是一个简单的示例来演示:

Window1.xaml

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button Name="b1">Show</Button>
        <Button Name="b2">ShowDialog</Button>
    </StackPanel>
</Window>

Window1.xaml.cs

using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            b1.Click += new RoutedEventHandler(b1_Click);
            b2.Click += new RoutedEventHandler(b2_Click);
        }

        void b1_Click(object sender, RoutedEventArgs e)
        {
            var w = new Window();
            w.Closed += delegate
            {
                MessageBox.Show("" + w.DialogResult);
            };

            w.Show();
        }

        void b2_Click(object sender, RoutedEventArgs e)
        {
            var w = new Window();
            w.ShowDialog();
            MessageBox.Show("" + w.DialogResult);
        }
    }
}

关闭窗口时,您会注意到对话框有一个 DialogResultfalse,而非对话框则具有 null DialogResult

The DialogResult property is defined on the Window class. Not all Windows are dialogs. Therefore, the property isn't relevant to all windows. A Window that has been shown via Show() rather than ShowDialog() will (presumably, unless you set it for some reason) have DialogResult = null.

Here's a simple example to demonstrate:

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button Name="b1">Show</Button>
        <Button Name="b2">ShowDialog</Button>
    </StackPanel>
</Window>

Window1.xaml.cs:

using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            b1.Click += new RoutedEventHandler(b1_Click);
            b2.Click += new RoutedEventHandler(b2_Click);
        }

        void b1_Click(object sender, RoutedEventArgs e)
        {
            var w = new Window();
            w.Closed += delegate
            {
                MessageBox.Show("" + w.DialogResult);
            };

            w.Show();
        }

        void b2_Click(object sender, RoutedEventArgs e)
        {
            var w = new Window();
            w.ShowDialog();
            MessageBox.Show("" + w.DialogResult);
        }
    }
}

When you close the windows, you'll notice that the dialog has a DialogResult of false, whilst the non-dialog has a null DialogResult.

平安喜乐 2024-07-30 08:24:16

在我看来,这样做是因为在大多数情况下,您不需要“重试”或“忽略”等通用的专用选项。

如果您需要的不仅仅是“确定/取消”,您应该使用某种任务对话框,例如带有书面答案的对话框。 这样,您就不会局限于几十年前有人想到的几个枚举值,并且 DialogResult 只是基本用途的正/负,您可以实现您自己的特定于您的高级需求的属性。 因此只需要 true/false,null 表示窗口还没有关闭(还没有给属性赋值)。

如果您的对话框不仅仅是用户应该回答的问题(例如输入表单),那么通常最好使用“确定”/“取消”,因此您不需要更多值。

In my opinion this was done because in most cases you don't need the generalized specialized options like Retry or Ignore.

If you need more than OK/Cancel, you are supposed to use some kind of task dialog, e.g. with written-out answers. That way, you're not limited to the few enum values someone thought of some decades ago, and the DialogResult is just positive/negative for basic use and you can implement your own property that is specific to your advanced needs. Therefore only true/false is needed, and null indicating that the window has not been closed yet (no value has been assigned to the property yet).

If you have a dialog that is more than just a question the user should answer (e.g. an entry form), you're typically better off with OK/Cancel, so you don't need more values.

一笑百媚生 2024-07-30 08:24:16

根据 MSDN 文档

对话框时DialogResult为空
框已显示,但既不被接受也不被接受
已取消。

但我不确定这是如何发生的,除非您正在处理访问对话框的多个线程。

当发生以下情况之一时,文档说是错误的:

  • 按 ALT+F4。
  • 单击“关闭”按钮。
  • 从“系统”菜单中选择“关闭”。

According to the MSDN documentation:

DialogResult is null when the dialog
box is shown but neither accepted nor
canceled.

But I'm not sure how that could happen unless you're dealing with multiple threads accessing the dialog.

The documentation says is false when one of the following things happen:

  • PressesALT+F4.
  • Clicks the Close button.
  • Selects Close from the System menu.
荒芜了季节 2024-07-30 08:24:16

IMO 这是因为 DialogResult 并不总是被使用。 你看,只有当窗口被它的 ShowDialog() 方法调用时,你才能设置 DialogResult,如果你用它的 Show() 方法调用它,并尝试将 DialogResult 设置为任何值,它将抛出 InvalidOperationException。 所以我认为这就是它可以为空的原因,如果你使用 Show() 方法调用窗口,它将为空,如果你使用 ShowDialog() 调用它,这取决于你。

IMO this is because DialogResult isn't always used. You see, you can only set DialogResult if the window is called by its ShowDialog() method, if you call it with its Show() method, and try to set DialogResult to anything, it'll throw an InvalidOperationException. So I think that is the reason it's nullable, in case you call the window with the Show() method, it'll be null, if you call it using ShowDialog(), it's up to you.

(り薆情海 2024-07-30 08:24:16

ShowDialog 将始终返回 true 或 false。 仅当对话框打开时,DialogResult 才会采用 null 状态。 从 null 转换为 true 或 false 将关闭对话框并使对 ShowDialog 的原始调用返回。

ShowDialog will always return true or false. DialogResult will only take the null state when the dialog is open. Transitioning from null to true or false will close the dialog and make the original call to ShowDialog return.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文