原文:
WPF判断当前窗体是否为模态
WPF判断当前窗体是否为模态
1、使用System.Windows.Interop.ComponentDispatcher.IsThreadModal来判断
注意事项:
1、Works not immediately after ShowModal is called. Some events still cannot tell if modal
2、This doesn't work if a modal dialog is showing this modeless dialog!
2、使用反射
eg:新建一个拓展方法
1 public static bool IsModal(this Window window) 2 { 3 return(bool)typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window); 4 }
改进为:
1 public static bool IsModal(this Window window) 2 { 3 var filedInfo=typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic); 4 5 return filedInfo!=null&&(bool)filedInfo.GetValue(window); 6 }
推荐第二种