0
votes

I am passing object of Window1 as reference to Window2 using this. Then I close Window1 and try to show Window1 using button click from Window2.
I am getting an error which is often possible with with any developer in wpf. Error is: Cannot set visibility or call Show, ShowDialog after a window has closed.

button click method in Window1.xaml.cs
{
Window2 w2 = new Window2();
w2.w1Obj = this;
w2.show();
}

Then I close Window1.

button click method in Window2.xaml.cs
{
w1Obj.testMethod();
this.w1Obj.show();
}

Here testMethod() is executed well but when I want to show that window, it gets crashed. so I want to know what happens in memory or whatever to Window1 object when I closed that Window.? Is this memory leak.?

1
Well, it looks like you are closing a window and then attempting to call show on it, which you can't do, as the error message clearly states.500 - Internal Server Error
but why.? If i can execute a method of that class, then I could show that window too.pratik03
See Luca's answer below. The class instance isn't being disposed since you hold a reference to it, but after Close has been called it is no longer a working form that can be shown again.500 - Internal Server Error

1 Answers

1
votes

You should try to use Window.Hide() instead of Window.Close().

When Window.Close() is called:

Closing a window causes the Closing event to be raised. If the Closing event isn't canceled, the following occurs:

The Window is removed from Application.Windows (if an Application object exists).

The Window is removed from the owner Window if the owner/owned relationship was established before the owned Window was shown and after the owner Window was opened.

The Closed event is raised.

Unmanaged resources created by the Window are disposed.

If ShowDialog was called to show the Window, ShowDialog returns.

Closing a Window causes any windows that it owns to be closed. Furthermore, closing a Window may cause an application to stop running depending on how the Application.ShutdownMode property is set.

You can find more on the Closing Event here Closing Event