WPF判断当前窗体是否为模态

WPF判断当前窗体是否为模态
 
1、使用System.Windows.Interop.ComponentDispatcher.IsThreadModal来判断
参照: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c95f1acb-5dee-4670-b779-b07b06afafff/where-is-modal-property?forum=wpf
注意事项:
        参照: http://stackoverflow.com/questions/368926/how-do-i-determine-if-a-wpf-window-is-modal
        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 }

 

参照: http://stackoverflow.com/questions/368926/how-do-i-determine-if-a-wpf-window-is-modal
 
推荐第二种

你可能感兴趣的:(WPF判断当前窗体是否为模态)