2021 年 WPF FolderBrowserDialog 是否需要 Owner Window?

Is Owner Window required in WPF FolderBrowserDialog in 2021?

在 WPF 中使用 FolderBrowserDialog 时,我看到一些答案要求使用 Win32 API 使其成为模态。例如,

Using FolderBrowserDialog in WPF application

How to use a FolderBrowserDialog from a WPF application

实际上,Win32 Api 真的需要吗?我刚刚尝试了 FolderBrowserDialog,默认情况下它已经是模态的;简单地调用 .ShowDialog() 已经是底层 window 的模式。 (其他答案在谈论什么或 wpf 内部结构发生了变化?)

编辑:使用 Visual Studio 2015 与 .NET 框架(非核心)

Actually, is the Win32 Api REALLY required

是的。 FolderBrowserDialog 通过 Win32 公开。如果不以某种方式使用 Win32,就无法为 Windows 编写软件。

I just tried the FolderBrowserDialog and it is already modal by default; simply calling .ShowDialog() is already modal to the underlying window.

那是因为当您调用 ShowDialog() 时,它会为所有者 hWnd 参数传递 IntPtr.Zero(与 C 中的 NULL 相同)。当您传递 NULL 时,Windows 使用进程中当前活动的 window 作为所有者 - 因此所有者仍然设置,只是自动确定:

From the documentation for WinForms' ShowDialog()(没有owner参数的重载。虽然WinForms和WPF是分开的,但两者的原理是一样的):

When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.

也就是说,您 应该 仍尽可能指定所有者 hWnd,因为您可能会重新构建您的应用程序并且可能希望另一个 window 成为所有者(例如,当你想在非根父 window 关闭后立即显示一个对话框)