C
C#2y ago
Gipper

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
HimmDawg
HimmDawg2y ago
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?
Jimmacle
Jimmacle2y ago
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
Gipper
GipperOP2y ago
If I have a window called say "thingA", in the app.xaml.cs I initialize the view associated with it:
public thingA View_thingA { get; private set; }
public App()
{
View_thingA = new thingA();
}
public thingA View_thingA { get; private set; }
public App()
{
View_thingA = new thingA();
}
then in the MainWindow.xaml.cs, when the respective button is clicked:
private void bt_thingA_Click(object sender, RoutedEventArgs e)
{
app.View_thingA.ShowDialog();
}
private void bt_thingA_Click(object sender, RoutedEventArgs e)
{
app.View_thingA.ShowDialog();
}
Jimmacle
Jimmacle2y ago
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 time
Gipper
GipperOP2y ago
In the button click event? How?
Jimmacle
Jimmacle2y ago
how do you think?
Gipper
GipperOP2y ago
private void bt_thingA_Click(object sender, RoutedEventArgs e)
{
View_thingA = new thingA();
app.View_thingA.ShowDialog();
}
private void bt_thingA_Click(object sender, RoutedEventArgs e)
{
View_thingA = new thingA();
app.View_thingA.ShowDialog();
}
???
Jimmacle
Jimmacle2y ago
you should not be accessing anything from whatever app is
Gipper
GipperOP2y ago
aah, ok, fixed thank you any idea why this happened with opening of some windows but not others?
Jimmacle
Jimmacle2y ago
no, but it seems like a bad pattern to be using
Gipper
GipperOP2y ago
yeah, it probably is in a perfect world I would like to do it better but oh well
Want results from more Discord servers?
Add your server