Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to a WPF window after it is closed?

in my WPF application I sometimes need to create a small helper window and I create the instance of the window the first time I need it.

if (mesareaderThreadQueWin == null)
{
    mesareaderThreadQueWin = new MesaReaderThreadQueWindow();    
}

mesareaderThreadQueWin.Show(); 

This works perfect so far. But if I close the window and invoke to code again the program crashes telling me I can't do show after the window already did close.

I wonder a little bit what exactly is happening here. The window is not null otherwise the code would just create a new instance but since I already have shown the window it seems like i can't do so again. So the window must be in some kind of in-between-state. Unable to become visible but not null.

Can I detect this state? Its there a way to reuse this window again other then not closing the window at all and using hide instead?

like image 352
TalkingCode Avatar asked May 02 '11 08:05

TalkingCode


People also ask

How do you check a window is closed in WPF?

Solution 1 The simplest solution is to handle the second forms Closed event in the first: when you create the Form2 instance, you add your event handler beofe ryou show the the form, and your handler will be executed when Form2 closes.

What is window closing?

The Window. close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.

Will WPF be discontinued?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.

Does WPF have a future?

This move clearly explains that Microsoft sees a future in WPF and considers it as a UI framework for the . NET platform. Microsoft has really invested a lot in WPF by making it open source and making it work on . NET Core.


2 Answers

Can I detect this state?

As far as I know, there is no way to access this state

Its there a way to reuse this window again other then not closing the window at all and using hide instead?

Yes, handle the Closing event in the dialog window, or override the OnClosing method:

protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;
    this.Hide();
}
like image 153
Thomas Levesque Avatar answered Oct 20 '22 09:10

Thomas Levesque


You could handle the Closing event, cancel it, then hide the window:

window.Closing += delegate(object sender, CancelEventArgs e)
{
    e.Cancel = true;
    window.Hide();
};

This ensures the Window never closes and allows you to call Show() any number of times.

It's probably superfluous given the above, but you could detect when your Window is closed by attaching to the Closed event and setting a flag there. That is, maintain your own isClosed variable.

like image 39
Kent Boogaart Avatar answered Oct 20 '22 07:10

Kent Boogaart