事件触发器(按钮)打开同一窗口的多个实例

时间:2023-01-18 19:14:01

In my WPF application, I have a main window (Branch.xaml) which has a button that will open an other window (Location.xaml). Once this Location window is open, how do I prevent another instance of this Location window from opening, when the user clicks the same button again?

在我的WPF应用程序中,我有一个主窗口(Branch.xaml),它有一个按钮,可以打开另一个窗口(Location.xaml)。打开此“位置”窗口后,当用户再次单击同一按钮时,如何阻止此“位置”窗口的另一个实例打开?

Or how can I re-focus the same open window, when the user clicks the button again?

或者,当用户再次单击按钮时,如何重新聚焦同一个打开的窗口?

The button click code is auto-generated code when you double-click on a button in xaml.

当您双击xaml中的按钮时,按钮单击代码是自动生成的代码。

In the "Branch.xaml.cs" file, the code for button click is as follows:

在“Branch.xaml.cs”文件中,按钮单击的代码如下:

private void rbtn_Location_Click(object sender, RoutedEventArgs e) 
{ 
    Location location = new Location(); 
    location.Show(); 
} 

Location is a custom class which opens a window with 3 list boxes

位置是一个自定义类,它打开一个包含3个列表框的窗口

Thanks, any help is appreciated. I'm using WPF application on C# 4.0 & Visual Studio 2010.

谢谢,任何帮助表示赞赏。我在C#4.0和Visual Studio 2010上使用WPF应用程序。

2 个解决方案

#1


0  

You could create a field in your main window which holds a reference to the location window if there is any, in the button click handler check if the field is null and if it is create a new window and store it in the field, if not call Activate on the window in the field. You also would have to subscribe to the Closed event of the location window to clear out the reference again when the location window is gone.

您可以在主窗口中创建一个字段,该字段包含对位置窗口的引用(如果有),在按钮单击处理程序中检查该字段是否为空以及是否创建新窗口并将其存储在字段中,如果没有调用激活字段中的窗口。当位置窗口消失时,您还必须订阅位置窗口的Closed事件以再次清除引用。

Edit: Concrete example:

编辑:具体示例:

private LocationWindow locationWindow;
private void Button1_Click(object sender, RoutedEventArgs e)
{
    if (locationWindow == null)
    {
        locationWindow = new LocationWindow();
        locationWindow.Closed += (s, _) => locationWindow = null;
        locationWindow.Show();
    }
    else
    {
        locationWindow.Activate();
    }
}

#2


0  

Application.Current.Windows collection holds reference for all windows for current AppDomain. You can check for your window in that collection and if it founds your window then call Activate for that window else create new Window. This will get you going -

Application.Current.Windows集合保存当前AppDomain的所有窗口的引用。您可以检查该集合中的窗口,如果它找到了您的窗口,则为该窗口调用Activate,否则创建新窗口。这会让你去 -

private void rbtn_Location_Click(object sender, RoutedEventArgs e) 
{ 
    Window window = Application.Current.Windows.OfType<Window>().Where(win => win.Name == "LocationWindow").FirstOrDefault();
    if(window == null)
    {
       Location location = new Location(); 
       location.Show(); 
    }
    else
    {
       window.Activate();  
    }
} 

Make sure you provide your window a x:Name as LocationWindow to make it work.

确保为窗口提供x:名称为LocationWindow以使其正常工作。

<Window x:Name="LocationWindow">
</Window>

Also include namespace System.Linq in your code behind.

在您的代码后面还包括命名空间System.Linq。

#1


0  

You could create a field in your main window which holds a reference to the location window if there is any, in the button click handler check if the field is null and if it is create a new window and store it in the field, if not call Activate on the window in the field. You also would have to subscribe to the Closed event of the location window to clear out the reference again when the location window is gone.

您可以在主窗口中创建一个字段,该字段包含对位置窗口的引用(如果有),在按钮单击处理程序中检查该字段是否为空以及是否创建新窗口并将其存储在字段中,如果没有调用激活字段中的窗口。当位置窗口消失时,您还必须订阅位置窗口的Closed事件以再次清除引用。

Edit: Concrete example:

编辑:具体示例:

private LocationWindow locationWindow;
private void Button1_Click(object sender, RoutedEventArgs e)
{
    if (locationWindow == null)
    {
        locationWindow = new LocationWindow();
        locationWindow.Closed += (s, _) => locationWindow = null;
        locationWindow.Show();
    }
    else
    {
        locationWindow.Activate();
    }
}

#2


0  

Application.Current.Windows collection holds reference for all windows for current AppDomain. You can check for your window in that collection and if it founds your window then call Activate for that window else create new Window. This will get you going -

Application.Current.Windows集合保存当前AppDomain的所有窗口的引用。您可以检查该集合中的窗口,如果它找到了您的窗口,则为该窗口调用Activate,否则创建新窗口。这会让你去 -

private void rbtn_Location_Click(object sender, RoutedEventArgs e) 
{ 
    Window window = Application.Current.Windows.OfType<Window>().Where(win => win.Name == "LocationWindow").FirstOrDefault();
    if(window == null)
    {
       Location location = new Location(); 
       location.Show(); 
    }
    else
    {
       window.Activate();  
    }
} 

Make sure you provide your window a x:Name as LocationWindow to make it work.

确保为窗口提供x:名称为LocationWindow以使其正常工作。

<Window x:Name="LocationWindow">
</Window>

Also include namespace System.Linq in your code behind.

在您的代码后面还包括命名空间System.Linq。