前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >直击本质:WPF 框架是如何实现模态窗口的

直击本质:WPF 框架是如何实现模态窗口的

作者头像
walterlv
发布2023-10-22 10:40:43
5590
发布2023-10-22 10:40:43
举报
文章被收录于专栏:walterlv - 吕毅的博客

想知道你在 WPF 编写 Window.ShowDialog() 之后,WPF 框架是如何帮你实现模态窗口的吗?

本文就带你来了解这一些。


Window.ShowDialog

WPF 显示模态窗口的方法就是 Window.ShowDialog,因此我们直接进入这个方法查看。由于 .NET Core 版本的 WPF 已经开源,我们会使用 .NET Core 版本的 WPF 源代码。

Window.ShowDialog 的源代码可以在这里查看:

这个方法非常长,所以我只把其中与模态窗口最关键的代码和相关注释留下,其他都删除(这当然是不可编译的):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

public Nullable<bool> ShowDialog() { // NOTE: // _threadWindowHandles is created here. This reference is nulled out in EnableThreadWindows // when it is called with a true parameter. Please do not null it out anywhere else. // EnableThreadWindow(true) is called when dialog is going away. Once dialog is closed and // thread windows have been enabled, then there no need to keep the array list around. // Please see BUG 929740 before making any changes to how _threadWindowHandles works. _threadWindowHandles = new ArrayList(); //Get visible and enabled windows in the thread // If the callback function returns true for all windows in the thread, the return value is true. // If the callback function returns false on any enumerated window, or if there are no windows // found in the thread, the return value is false. // No need for use to actually check the return value. UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(), new NativeMethods.EnumThreadWindowsCallback(ThreadWindowsCallback), NativeMethods.NullHandleRef); //disable those windows EnableThreadWindows(false); try { _showingAsDialog = true; Show(); } catch { // NOTE: // See BUG 929740. // _threadWindowHandles is created before calling ShowDialog and is deleted in // EnableThreadWindows (when it's called with true). // // Window dlg = new Window(); // Button b = new button(); // b.OnClick += new ClickHandler(OnClick); // dlg.ShowDialog(); // // // void OnClick(...) // { // dlg.Close(); // throw new Exception(); // } // // // If above code is written, then we get inside this exception handler only after the dialog // is closed. In that case all the windows that we disabled before showing the dialog have already // been enabled and _threadWindowHandles set to null in EnableThreadWindows. Thus, we don't // need to do it again. // // In any other exception cases, we get in this handler before Dialog is closed and thus we do // need to enable all the disable windows. if (_threadWindowHandles != null) { // Some exception case. Re-enable the windows that were disabled EnableThreadWindows(true); } } }

觉得代码还是太长?不要紧,我再简化一下:

  1. EnumThreadWindows 获取当前线程的所有窗口
  2. 把当前线程中的所有窗口都禁用掉(用的是 Win32 API 的禁用哦,这不会导致窗口内控件的样式变为禁用状态)
  3. 将窗口显示出来(如果出现异常,则还原之前禁用的窗口)

可以注意到禁用掉的窗口是“当前线程”的哦。

ShowHelper

接下来的重点方法是 Window.ShowDialog 中的那句 Show()。在 Show() 之前设置了 _showingAsDialogtrue,于是这里会调用 ShowHelper 方法并传入 true

下面的代码也是精简后的 ShowHelper 方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

private object ShowHelper(object booleanBox) { try { // tell users we're going modal ComponentDispatcher.PushModal(); _dispatcherFrame = new DispatcherFrame(); Dispatcher.PushFrame(_dispatcherFrame); } finally { // tell users we're going non-modal ComponentDispatcher.PopModal(); } }

可以看到,重点是 PushModalPopModal 以及 PushFrame

PushFrame 的效果就是让调用 ShowDialog 的代码看起来就像阻塞了一样(实际上就是阻塞了,只不过开了新的消息循环看起来 UI 不卡)。

关于 PushFrame 为什么能够“阻塞”你的代码的同时还能继续响应 UI 操作的原理,可以阅读:

那么 ComponentDispatcher.PushModalComponentDispatcher.PopModal 呢?可以在这里(ComponentDispatcherThread.cs)看它的代码,实际上是为了模态计数以及引发事件的,对模态的效果没有本质上的影响。

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/how-does-wpf-implement-modal-dialog.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Window.ShowDialog
  • ShowHelper
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档