ShowDialog() 什么时候返回 null? [英] When would ShowDialog() return null?

查看:51
本文介绍了ShowDialog() 什么时候返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 的 Window.ShowDialog 方法返回一个可为空的布尔值.CommonDialog.ShowDialog也是如此.>

现在,我了解这些将返回 false(用户单击取消或按 Esc)以及何时返回 true 的情况(代码将 Window.DialogResult 设置为 true,可能是为了响应 OK单击).但是为空?

我的第一个想法是单击标题栏的关闭按钮可能会返回 null.但是文档声明(我通过测试确认)标题栏关闭按钮被视为取消.

那么 Window.ShowDialogCommonDialog.ShowDialog 什么时候会返回 null?

解决方案

该方法总是返回 true 或 false,这总是等于窗口关闭时的 DialogResult 属性.

但是在窗口关闭之前DialogResult属性为null,另一个线程可以检查该属性.因此,返回值是一个可以为空的布尔值来匹配该属性是有道理的,即使它实际上从未为空.

WPF's Window.ShowDialog method returns a nullable boolean. So does CommonDialog.ShowDialog.

Now, I understand cases where these would return false (user clicked Cancel or pressed Esc), and when they would return true (code sets Window.DialogResult to true, probably in response to OK being clicked). But null?

My first thought is that clicking the title bar's Close button might return null. But the docs state (and I confirmed by testing) that the title-bar Close button is treated as a Cancel.

So when would Window.ShowDialog or CommonDialog.ShowDialog ever return null?

解决方案

The method always returns true or false, and this is always equal to the DialogResult property of the window at the time it closes.

But the DialogResult property is null before the window is closed, and another thread could check the property. So it kind of makes sense that the return value is a nullable boolean to match the property, even though it is never actually null.

这篇关于ShowDialog() 什么时候返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆