I have created single instance application. It accepts command line arguments and process them. If application is already running and some dialog ( open file or message box ) is opened. Now if i try to pass command line argument i need to check if dialog is shown or not. So I added this code.
if (!Application.Current.MainWindow.IsActive)
{
Application.Current.MainWindow.Activate();
}
if (Keyboard.FocusedElement != null)
{
// If focused element is not null it means no other dialog is shown.
// safe to go.
}
Ideal was like , if focused element is not null then it means focus is inside window and no other dialog is shown.
In normal scenarios when window is not minimized this code works fine. but if window is minimized then condition fails as keyboard focus is not in window.
Do u find any solution which will be generic ? I can achieve thi开发者_StackOverflow社区s by adding flags before each dialog box. but I have more than 10 dialog boxes. In future i may add more dialog boxes.
Thanks
Very old question but I've recently faced a similar issue. Here is how I solved it:
public static bool IsAnyModalOpened()
{
return Application.Current.Windows.OfType<Window>().Any(IsModal);
}
public static bool IsModal(this Window window)
{
var fieldInfo = typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
return fieldInfo != null && (bool)fieldInfo.GetValue(window);
}
精彩评论