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?
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.
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.
“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.
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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With