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

时间:2015-06-02 19:47:16   收藏:0   阅读:782
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
 
推荐第二种

原文:http://www.cnblogs.com/6112562a/p/4547092.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!