检测是否打开任何对话框

发布于 2024-12-08 22:41:48 字数 617 浏览 0 评论 0原文

我创建了单实例应用程序。它接受命令行参数并处理它们。如果应用程序已在运行并且打开了某个对话框(打开文件或消息框)。现在,如果我尝试传递命令行参数,我需要检查是否显示对话框。所以我添加了这段代码。

        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.
        }

理想情况是,如果焦点元素不为空,则意味着焦点位于窗口内并且不显示其他对话框。

在正常情况下,当窗口未最小化时,此代码可以正常工作。但如果窗口最小化,则条件失败,因为键盘焦点不在窗口中。

您找到通用的解决方案吗?我可以通过在每个对话框之前添加标志来实现这一点。但我有超过10个对话框。将来我可能会添加更多对话框。

谢谢

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 this by adding flags before each dialog box. but I have more than 10 dialog boxes. In future i may add more dialog boxes.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-12-15 22:41:48

很老的问题,但我最近遇到了类似的问题。
我是这样解决的:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文