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 }
推薦第二種

浙公網安備 33010602011771號