How to determine if a window is

I have a function in WPF MainWindow1.Xaml when the application shuts down. But in unknown cases, the user can close the application in another way, and then use the "close file" button. I just need to know in the Close method if the current MainWindow1.xaml is already installed or not. But I could not find any property.

Can you help me?

+4
source share
2 answers

You can try

var source = PresentationSource.FromVisual(yourWindow) 

If source is null or source.IsDisposed == true , then your window has been closed.

+5
source

You can attach to the Closed event on the Window . When this event is called, the windows close . Actually, I have to fix it: according to the MSDN documentation , this means that you cannot prevent the window from closing when this event is called.
If listening to the Closed event is not good enough, this is another option: How do I know if the WPF window is closed?

+1
source

All Articles