为什么 DialogResult 在 WPF 中是可为 null 的 bool?
有人能想到一个好的解释来解释 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DialogResult
属性在Window
类上定义。 并非所有Window
都是对话框。 因此,该属性并不与所有窗口相关。 通过Show()
而不是ShowDialog()
显示的Window
将(大概,除非您出于某种原因设置它)具有 <代码>DialogResult = null。下面是一个简单的示例来演示:
Window1.xaml:
Window1.xaml.cs:
关闭窗口时,您会注意到对话框有一个
DialogResult
为false
,而非对话框则具有null DialogResult
。The
DialogResult
property is defined on theWindow
class. Not allWindow
s are dialogs. Therefore, the property isn't relevant to all windows. AWindow
that has been shown viaShow()
rather thanShowDialog()
will (presumably, unless you set it for some reason) haveDialogResult = null
.Here's a simple example to demonstrate:
Window1.xaml:
Window1.xaml.cs:
When you close the windows, you'll notice that the dialog has a
DialogResult
offalse
, whilst the non-dialog has anull DialogResult
.在我看来,这样做是因为在大多数情况下,您不需要“重试”或“忽略”等通用的专用选项。
如果您需要的不仅仅是“确定/取消”,您应该使用某种任务对话框,例如带有书面答案的对话框。 这样,您就不会局限于几十年前有人想到的几个枚举值,并且 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.
根据 MSDN 文档:
但我不确定这是如何发生的,除非您正在处理访问对话框的多个线程。
当发生以下情况之一时,文档说是错误的:
According to the MSDN documentation:
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:
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.
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.