检测是否打开任何对话框
我创建了单实例应用程序。它接受命令行参数并处理它们。如果应用程序已在运行并且打开了某个对话框(打开文件或消息框)。现在,如果我尝试传递命令行参数,我需要检查是否显示对话框。所以我添加了这段代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很老的问题,但我最近遇到了类似的问题。
我是这样解决的:
Very old question but I've recently faced a similar issue.
Here is how I solved it: