Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to ask if a Window is open in WPF

Tags:

c#

window

wpf

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)

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!=null && cm.IsLoaded)
{
    cm.Canales.setValues();
}

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

like image 685
Naty Bizz Avatar asked Oct 29 '13 13:10

Naty Bizz


People also ask

How do you call a window in WPF?

Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK. Step 2: Create a button using the following code or drag and drop a button from the ToolBox of Visual Studio 2013.

Is WPF deprecated?

I think WPF will be dead in 2022 because its framework no longer supports new development, and Microsoft is pushing hard for a new cross-platform development framework, including the UNO platform and Xamarin. Electron is also more progressive compared to UWP.

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

What is difference between user control and window in WPF?

A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.


2 Answers

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

What you have to check is the Visibility property (it's what, finally, Show() will change), it'll be Visible if the Window is currently visible or Hidden if it has not been loaded (or it has been loaded and it still is but actually hidden).

To summarize:

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

Please note that if Window is 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).

like image 74
Adriano Repetti Avatar answered Nov 12 '22 00:11

Adriano Repetti


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

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

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

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

Your Window will not appear here before it is displayed.

like image 45
Sheridan Avatar answered Nov 11 '22 22:11

Sheridan