Why is DialogResult a nullable bool in WPF?

后端 未结 5 768
逝去的感伤
逝去的感伤 2021-01-17 08:59

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 m

5条回答
  •  萌比男神i
    2021-01-17 09:11

    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.

提交回复
热议问题