开发者

Detect if any dialog is opened

开发者 https://www.devze.com 2023-04-11 09:51 出处:网络
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 t

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号