开发者

ShowDialog return type: Form vs. Window

开发者 https://www.devze.com 2023-02-09 03:12 出处:网络
In WPF / .Net, both System.Windows.Window and System.Windows.Forms.Form have the .ShowDialog method. The difference is that the Windows.Window version returns a nullable bool (bool? or Nullable<bo

In WPF / .Net, both System.Windows.Window and System.Windows.Forms.Form have the .ShowDialog method.

The difference is that the Windows.Window version returns a nullable bool (bool? or Nullable<bool>), while the Forms.Form version returns a straight bool.

I haven't yet seen the nullable bool ever be null, and cannot find a case where it would be in the MSDN documentation.

Can anyone explain why Windows.Window.ShowDialog returns a nullable? Should I check for it开发者_运维问答? What does it mean if I see null?


see here: Window.DialogResult Property

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


As Kent Boogaart demonstrates in his answer to a very similar question

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

So Windows.Window.ShowDialog returns a nullable because it uses the DialogResult property which is null when its not a dialog


why Windows.Window.ShowDialog returns a nullable?

I think that was just an arbitrary implementation choice. If I were to guess, I would say that returning a nullable bool is a little more lightweight than creating another enum to return the result. There are three states for a nullable boolean, an that covers all of the bases.


ShowDialog returns the DialogResult of the window, which is a Nullable. While the window is open, DialogResult is null. Once DialogResult is set to true or false, ShowDialog will return that result. Since any close operation on the window will implicitly set DialogResult to false unless it is explicitly set, yes, you can safely ignore null values returned by ShowDialog (DialogResult, on the other hand, can be null if you check it directly). This is simply a result of matching the types exactly, as far as I know.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号