询问窗口是否在 WPF 中打开的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19660337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:58:37  来源:igfitidea点击:

Proper way to ask if a Window is open in WPF

c#wpfwindow

提问by Naty Bizz

I have this click event in my main window to open a new window

我的主窗口中有这个点击事件来打开一个新窗口

private void Button_Click(object sender, RoutedEventArgs e)
{
    cm = new CanalesMain();

    cm.Show();

    cm.Canales.setValues();

}

My cm variable is defined as a member class in my main window because I need to load/refresh the setValues() method every 5 minutes (there's a TimeSpan and a EventHandler for that)

我的 cm 变量被定义为主窗口中的成员类,因为我需要每 5 分钟加载/刷新一次 setValues() 方法(有一个 TimeSpan 和一个 EventHandler)

The thing is, in my "refresh data" method I have this if statement to ask if the cm variable is loaded and is not null (I mean, if the window was ever opened or if is opened, ask if isn't closed)

问题是,在我的“刷新数据”方法中,我有这个 if 语句来询问 cm 变量是否已加载并且不为空(我的意思是,如果窗口曾经打开或打开,询问是否未关闭)

if (cm!=null && cm.IsLoaded)
{
    cm.Canales.setValues();
}

Is this the correct or best way to ask if my window is open?

这是询问我的窗户是否打开的正确或最佳方式吗?

回答by Adriano Repetti

Strictly speaking no, it's not the right way. IsLoadeddoesn't mean that Windowis visible, just loaded (even if this maybe equivalent in most of scenarios but it means this window has been created once, it has a handle, no mention to its visibility).

严格来说不,这不是正确的方法。IsLoaded并不意味着它Window是可见的,只是加载了(即使这在大多数情况下可能是等效的,但这意味着此窗口已创建一次,它有一个句柄,没有提及其可见性)。

What you have to check is the Visibilityproperty (it's what, finally, Show()will change), it'll be Visibleif the Windowis currently visible or Hiddenif it has not been loaded (or it has been loaded and it still is but actually hidden).

你必须检查的是Visibility属性(它是什么,最后,Show()将改变),如果当前可见或尚未加载(或已加载但仍然是),它将是可见的实际上是隐藏的)。WindowHidden

To summarize:

总结一下:

if (cm != null && cm.Visibility == Visibility.Visible)
{
}

Please note that if Windowis visible then it's implicit it has been loaded (it has a handle) but it's not true the vice-versa (a loaded window may not be visible and maybe it was even not in the past).

请注意,如果Window是可见的,则它是隐式的,它已被加载(它有一个句柄),但反之亦然(加载的窗口可能不可见,甚至过去可能不可见)。

回答by Sheridan

There is another way of checking which Windows are currently active:

还有另一种方法可以检查哪些Windows 当前处于活动状态:

foreach (Window window in Application.Current.Windows)
{ 
    // Check for your Window here
}

If your Windowis of a particular type, then you can do this instead:

如果您Window是特定类型,那么您可以改为执行此操作:

foreach (Window window in Application.Current.Windows.OfType<YourWindow>())
{ 
    // Do something with your Window here
}

Your Windowwill not appear here before it is displayed.

Window在显示之前不会出现在此处。

回答by testCoder

I think extension method will be very useful in that case, try this:

我认为扩展方法在这种情况下会非常有用,试试这个:

public static class WindowsExtensions
{
   public static bool IsOpened(this Window window)
   {
        return Application.Current.Windows.Cast<Window>().Any(x => x.GetHashCode() == window.GetHashCode());
   }
}

Which gives you ability to make call on every window like this:

这使您能够像这样在每个窗口上进行调用:

var wind = new ChildWindow();
wind.ShowDialog();
var isOpened = wind.IsOpened();

Also you can check out this: How do I know if a WPF window is opened

你也可以看看这个:我怎么知道 WPF 窗口是否打开

More info about Application.Windows

有关Application.Windows 的更多信息

回答by Arnel

If you call myWindow.Show() and myWindow.Close(), myWindow.IsLoaded should get you a value that you can use to indicate if the window is open or not.

如果你调用 myWindow.Show() 和 myWindow.Close(),myWindow.IsLoaded 应该给你一个值,你可以用它来指示窗口是否打开。