Windows crashing the program when attempting to reopen windows after having been closed once already
My MainWindow has some buttons which open other windows also in the project through a
app.view_name.ShowDialog();
command. If I click the button to open the problem windows and then close it, if I attempt to reopen by clicking the button once again I get this exception:
System.InvalidOperationException: 'Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.'
.
Of the four buttons in this MainWindow two of them are experiencing this crash, while the others aren't for some reason. Please tell me if there's any code you would like to see as I am more than happy to provide it, but there's just so much code that could be relevant to my inexperienced eyes I didn't know what to post.11 Replies
The exception message says it all:
"Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."
How do you open those windows?
it sounds like one instance of the dialog is sitting around in a singleton/static class
if you're trying to show a new window you should create a new window
If I have a window called say "thingA", in the app.xaml.cs I initialize the view associated with it:
then in the MainWindow.xaml.cs, when the respective button is clicked:
yeah don't do that
you can't reopen the same instance of a window you've already closed, which is exactly what the exception is telling you
you need to create a new
thingA
each timeIn the button click event?
How?
how do you think?
???
you should not be accessing anything from whatever
app
isaah, ok, fixed thank you
any idea why this happened with opening of some windows but not others?
no, but it seems like a bad pattern to be using
yeah, it probably is
in a perfect world I would like to do it better but oh well