C# 如何知道 WPF 窗口是否打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16202101/
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
How do I know if a WPF window is opened
提问by guilhermecgs
In a WPF window, how do I know if it is opened?
在 WPF 窗口中,如何知道它是否已打开?
My goal to open only 1 instance of the window at the time.
我的目标是当时只打开 1 个窗口实例。
So, my pseudo code in the parent window is:
所以,我在父窗口中的伪代码是:
if (this.m_myWindow != null)
{
if (this.m_myWindow.ISOPENED) return;
}
this.m_myWindow = new MyWindow();
this.m_myWindow.Show();
EDIT:
编辑:
I found a solution that solves my initial problem. window.ShowDialog();
我找到了解决我最初问题的解决方案。window.ShowDialog();
It blocks the user from opening any other window, just like a modal popup. Using this command, it is not necessary to check if the window is already open.
它阻止用户打开任何其他窗口,就像模式弹出窗口一样。使用此命令,无需检查窗口是否已打开。
采纳答案by sa_ddam213
In WPFthere is a collection of the open Windowsin the Applicationclass, you could make a helper method to check if the window is open.
在WPF有开放的集合Windows的Application类,你可以做检查的辅助方法,如果窗口开启。
Here is an example that will check if any Windowof a certain Typeor if a Windowwith a certain name is open, or both.
这是一个示例,将检查某个特定名称中的任何Window一个Type或Window具有特定名称的 a 是否已打开,或两者都打开。
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
Usage:
用法:
if (Helpers.IsWindowOpen<Window>("MyWindowName"))
{
// MyWindowName is open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>())
{
// There is a MyCustomWindowType window open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>("CustomWindowName"))
{
// There is a MyCustomWindowType window named CustomWindowName open
}
回答by jure
You can check if m_myWindow==nulland only then create and show window. When the window closes set the variable back to null.
您可以检查是否m_myWindow==null且仅然后创建并显示窗口。当窗口关闭时,将变量设置回 null。
if (this.m_myWindow == null)
{
this.m_myWindow = new MyWindow();
this.m_myWindow.Closed += (sender, args) => this.m_myWindow = null;
this.m_myWindow.Show();
}
回答by It'sNotALie.
Put a static bool in your class, named _openor something like that.
In the constructor then do this:
在你的类中放置一个静态布尔值,命名_open或类似的东西。在构造函数中然后执行以下操作:
if (_open)
{
throw new InvalidOperationException("Window already open");
}
_open = true;
and in the Closed event:
并在 Closed 事件中:
_open = false;
回答by Floc
Is that you search ?
是你搜索吗?
if (this.m_myWindow != null)
{
if (this.m_myWindow.IsActive) return;
}
this.m_myWindow = new MyWindow();
this.m_myWindow.Show();
If you want a singleton, you should read this : How can we create a Singleton Instance for a Window?
如果你想要一个单例,你应该阅读这个:我们如何为一个窗口创建一个单例实例?
回答by Mauricio Ferraz
If you need to activate the window if it is found, you can use the code below:
如果发现需要激活窗口,可以使用下面的代码:
//activate a window found
//T = Window
Window wnd = Application.Current.Windows.OfType<T>().Where(w => w.Name.Equals(nome)).FirstOrDefault();
wnd.Activate();
回答by Ugur Mahmut Ye?ilyurt
void pencereac<T> (int Ops) where T : Window , new()
{
if (!Application.Current.Windows.OfType<T>().Any()) // Check is Not Open, Open it.
{
var wind = new T();
switch (Ops)
{
case 1:
wind.ShowDialog();
break;
case 0:
wind.Show();
break;
}
}
else Application.Current.Windows.OfType<T>().First().Activate(); // Is Open Activate it.
}
Kullan?m?:
库兰?m?:
void Button_1_Click(object sender, RoutedEventArgs e)
{
pencereac<YourWindow>(1);
}
回答by Mike Eason
Here is another way to achieve this using LINQ.
这是使用 LINQ 实现此目的的另一种方法。
using System.Linq;
...
public static bool IsOpen(this Window window)
{
return Application.Current.Windows.Cast<Window>().Any(x => x == window);
}
Usage:
用法:
bool isOpen = myWindow.IsOpen();
回答by shreekanth m
public bool IsWindowOpen<T>(string name = "") where T : Window
{
return Application.Current.Windows.OfType<T>().Any(w => w.GetType().Name.Equals(name));
}
回答by Chad
This works if you want to Check if the Second Form is already Open and avoidt opening it again on buttong Click.
如果您想检查第二个表单是否已经打开并避免在按钮单击时再次打开它,这将起作用。
int formcheck = 0;
private void button_click()
{
Form2Name myForm2 = new Form2Name();
if(formcheck == 0)
{
myForm2.Show(); //Open Form2 only if its not active and formcheck == 0
// Do Somethin
formcheck = 1; //Set it to 1 indicating that Form2 have been opened
{
{

