简体   繁体   English

ShowDialog() 什么时候返回 null?

[英]When would ShowDialog() return null?

WPF's Window.ShowDialog method returns a nullable boolean. WPF 的Window.ShowDialog方法返回一个可为空的布尔值。 So does CommonDialog.ShowDialog . 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).现在,我了解这些将返回 false(用户单击 Cancel 或按 Esc)以及何时返回 true(代码将Window.DialogResult设置为 true,可能是为了响应单击 OK)的情况。 But null?但是空?

My first thought is that clicking the title bar's Close button might return null.我的第一个想法是单击标题栏的关闭按钮可能会返回 null。 But the docs state (and I confirmed by testing) that the title-bar Close button is treated as a Cancel .但是文档声明(我通过测试确认) 标题栏关闭按钮被视为 Cancel

So when would Window.ShowDialog or CommonDialog.ShowDialog ever return null?那么Window.ShowDialogCommonDialog.ShowDialog什么时候会返回 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. 该方法始终返回true或false,并且它始终等于窗口关闭时的DialogResult属性。

But the DialogResult property is null before the window is closed, and another thread could check the property. 但是在关闭窗口之前,DialogResult属性为null,而另一个线程可以检查该属性。 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. 因此,有意义的是,返回值是一个可以为空的布尔值来匹配属性,即使它实际上从不为null。

If I return DialogResult = null in the Click event for a button, the window remains open. 如果我在按钮的Click事件中返回DialogResult = null,则窗口保持打开状态。

private void OkButton_Click(object sender, RoutedEventArgs e)
{
   Button btn = sender as Button;
   if ( btn != null )
   {
       // forces all control to update...
       btn.Focus();
   }

   // TEST IF OK TO CLOSE
   bool rc = _vm.ProcessOkCommand();
   if (rc)
   {
      DialogResult = true;
   }
   else
   {
      DialogResult = null;
   }
}


<Button Content="OK" Name ="cmdOK" IsDefault="True" Click="OkButton_Click"/>

I can give you an example I just encountered. 我可以举一个我刚遇到的例子。 Window.ShowDialog() will return null when you perform the following steps: 执行以下步骤时, Window.ShowDialog()将返回null:

  • You first close all of your Application 's windows. 首先关闭所有Application的窗口。
  • All other Window objects that have been instantiated up until now with the new keyword are closed. 到目前为止,使用new关键字实例化的所有其他Window对象都将关闭。
  • You try to instantiate a new Window and try calling Window.ShowDialog() on it. 您尝试实例化一个新Window并尝试在其上调用Window.ShowDialog() It will return null. 它将返回null。

This is because, presumably, you have no existing Window under which your new dialog can bind to in order to behave like a dialog which owns the topmost window state. 这是因为,假设您没有现有的Window ,您的新对话框可以绑定到该窗口,以便像拥有最顶层窗口状态的对话框一样运行。

A call to window.ShowDialog() can return null in very special circumstances, which I ran into by chance:在非常特殊的情况下,对window.ShowDialog()调用可能会返回 null,这是我偶然遇到的:

If the window.Closing event is caught in the following manner如果window.Closing事件被以下方式捕获

        var window = new DialogTestWindow();
        window.Closing += (o, e) => { e.Cancel = true; window.Hide(); };
        MessageBox.Show(window.ShowDialog().ToString());

then setting DialogResult = true or DialogResult = false from the dialog window will cause the window.ShowDialog() call to return null.然后从对话窗口设置DialogResult = trueDialogResult = false将导致window.ShowDialog()调用返回 null。 Calling Hide() from the dialog window will cause it to return false.从对话窗口调用Hide()将导致它返回 false。

Edit: The comments in the source code to Window clearly say that the intention is that a call to ShowDialog() should never return null.编辑: Window 源代码中的注释清楚地表明,对ShowDialog()的调用永远不应返回 null。 However, when Hide() is called from within the Closing event, the various checks that should prevent this fail: Hide() sets _dialogResult to false, but a check for whether the closing of the window has been cancelled subsequently sets it to null.但是,当从Closing事件中调用 Hide() 时,应防止这种情况发生的各种检查失败: Hide()_dialogResult设置为 false,但随后检查窗口关闭是否已取消将其设置为 null。

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

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