关闭所有打开的模态对话框窗口 [英] Close all open modal dialog windows

查看:41
本文介绍了关闭所有打开的模态对话框窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序,它有几个用于各种目的的模态窗口.这可以通过使用 ShowDialog 函数轻松实现.但是,在我的应用程序中,我有一个计时器来测量将导致用户注销的空闲时间(即没有鼠标移动或击键).有没有办法(当这个计时器触发时)找到并关闭所有打开的模态窗口而不显式跟踪每个窗口?

I have a WPF application that has several modal window used for various purposes. This is easily accomplished by using the ShowDialog function. However, in my application I have a timer to measure idle time (i.e. no mouse moves or key strokes) that will cause the user to be logged off. Is there a way (when this timer fires) to find and close all open modal windows without tracking each explicitly?

更新我还想关闭任何 MessageBox.Show 实例.这可能吗?

Update I would also like to close any MessageBox.Show instances. Is this possible?

谢谢,马特

推荐答案

有没有办法(当这个计时器触发时)找到并关闭所有打开的模态窗口而不显式跟踪每个窗口?

Is there a way (when this timer fires) to find and close all open modal windows without tracking each explicitly?

您可以使用 ComponentDispatcher.IsThreadModal 检查您的 UI 线程是否处于模态状态.如果是,Application.Current.Windows 属性将为您提供打开的 Windows 列表.

You could use ComponentDispatcher.IsThreadModal to check to see if you're UI thread is in a modal state. If it is, the Application.Current.Windows property will give you the list of opened Windows.

如果您只有一个 MainWindow,您可以关闭任何其他窗口(因为它们将是您的模态对话框),但如果您有多个窗口,则必须检查每个窗口.

If you only have a single MainWindow, you could close any others (as they'd be your modal dialogs), but if you have multiple windows, you'd have to check each one.

不幸的是,没有直接的 API 来确定特定的 Window 是否是模态的 - 但是 Window 类中有一个私有变量可以用来执行此操作.例如,下面的方法使用反射来确定一个 Window 是否是模态的:

Unfortunately, there's no direct API to determine whether a specific Window is modal - but there is a private variable in the Window class you could use to do this. For example, the following method uses reflection to determine whether a Window is modal:

  public static bool IsModal(Window window)
  {
       Type type = typeof(Window);
       var field = type.GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
       return field.GetValue(window);
  }

不幸的是,这可能会发生变化(因为它使用的是无证私人成员).

This is, unfortunately, subject to change (since it's using undocumented private members).

这篇关于关闭所有打开的模态对话框窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆