[英]How do I know if a WPF window is opened
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.
使用此命令,无需检查窗口是否已打开。
In WPF
there is a collection of the open Windows
in the Application
class, you could make a helper method to check if the window is open.在
WPF
中, Application
类中有一个打开Windows
的集合,您可以创建一个辅助方法来检查窗口是否打开。
Here is an example that will check if any Window
of a certain Type
or if a Window
with a certain name is open, or both.这是一个示例,将检查是否有某种
Type
任何Window
或具有特定名称的Window
是否已打开,或两者都打开。
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
}
You can check if m_myWindow==null
and only then create and show window.您可以检查
m_myWindow==null
是否然后创建并显示窗口。 When the window closes set the variable back to 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();
}
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();
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();
Put a static bool in your class, named _open
or something like that.在你的类中放置一个静态布尔值,命名为
_open
或类似的东西。 In the constructor then do this:在构造函数中然后执行以下操作:
if (_open)
{
throw new InvalidOperationException("Window already open");
}
_open = true;
and in the Closed event:并在 Closed 事件中:
_open = false;
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?如果你想要一个单例,你应该阅读这个: 我们如何为一个窗口创建一个单例实例?
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ı:库拉尼米:
void Button_1_Click(object sender, RoutedEventArgs e)
{
pencereac<YourWindow>(1);
}
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
{
{
public bool IsWindowOpen<T>(string name = "") where T : Window
{
return Application.Current.Windows.OfType<T>().Any(w => w.GetType().Name.Equals(name));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.