WPF: Cannot reuse window after it has been closed

wpf check if window is closed
cannot set owner property to a window that has not been shown previously
cannot set owner property to a window that has been closed
wpf window close event
wpf reopen closed window
wpf close window
window close not working wpf
wpf window closed state

I am trying to keep one instance of a Window around and when needed call ShowDialog. This worked find in winforms, but in WPF I recieve this exeception:

System.InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

Is there any way to do something like this in WPF?

MyWindow.Instance.ShowDialog();

public class MyWindow : Window
{
    private static MyWindow _instance;

    public static MyWindow Instance
    {
        if( _instance == null )
        {
            _instance = new Window();
        }
        return _instance();
    }
}

I suppose you could do it if you changed visibility of the window rather than closing it. You'd need to do that in the Closing() event and then cancel the close. If you allow the close to happen you certainly can't reopen a closed window - from here:

If the Closing event isn't canceled, the following occurs:

...

Unmanaged resources created by the Window are disposed.

After that happens the window will never be valid again.

I don't think it's worth the effort though - it really isn't that much of a performance hit to make a new window each time and you are far less likely to introduce hard to debug bugs / memory leaks. (Plus you'd need to make sure that it did close and release it's resources when the application is shut down)


Just read that you are using ShowDialog(), this will make the window modal and simply hiding it won't return control to the parent window. I doubt it is possible to do this at all with modal windows.

[SOLVED] WPF: Cannot reuse window after it has been closed , I suppose you could do it if you changed visibility of the window rather than closing it. You'd need to do that in the Closing() event and then� Questions: I am trying to keep one instance of a Window around and when needed call ShowDialog. This worked find in winforms, but in WPF I recieve this exeception: System.InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

If I'm not wrong, you can cancel the closing event of that window and instead set visibility to hidden

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    } 

what happens behind the seen on closing window in C# wpf, When you close a window the object is disposed. and you cannot re-open a window instance that has previously been closed. If If you need to reuse Window1 override OnClosing, cancel the close and just hide it instead. When we try to show the Window which is closed, we will get the following exception. "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed." Recommend:c# - Why isn't WPF Window automatically disposed and collected after MainWindow closed. ue, simply: Add a new Window in a new WPF

if you cancel the close event and set visibility =hidden then you can override this issue

Private Sub ChildWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        e.Cancel = True
        Me.Visibility = Windows.Visibility.Hidden
End Sub

Reopen a window in WPF - MSDN, reopen, i get an exception "Cannot set Visibility or call Show or ShowDialog after window has closed". Is it possible to reuse such a window? "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed." So to handle this case it would be better if we use Visibility option of the window. We need to set the visibility of the window to Hidden or Collapsed instead of closing it directly.

When we try to show the Window which is closed, we will get the following exception.

"Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."

So to handle this case it would be better if we use Visibility option of the window. We need to set the visibility of the window to Hidden or Collapsed instead of closing it directly.

this.Visibility = System.Windows.Visibility.Collapsed or Hidden;

If we want to show it again, just set the visibility to Visible

this.Visibility = System.Windows.Visibility.Visible;

WPF Window Closing Errors, WPF:关闭后不能重用窗口. [英]WPF: Cannot reuse window after it has been closed. 本文翻译自 Jerod Houghtelling 查看原文 2010-08-25 48059 c#/ singleton/ � Settings button click is handeled by Config(), it show settings windows when clicked. If I close the using cross sign on the right corner of window, I am not able to reuse the windows it shows following exception. Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

public class MyWindow : Window

public MyWindow ()
    {
        InitializeComponent();            
        Closed += new System.EventHandler(MyWindow_Closed);
    }

private static MyWindow _instance;

public static MyWindow Instance
{
    if( _instance == null )
    {
        _instance = new Window();
    }
    return _instance();
}
void MyWindow_Closed(object sender, System.EventArgs e)
    {
         _instance = null;
    }

WPF:关闭后不能重用窗口- WPF: Cannot reuse window after it has , This is what it is. Practically, you cannot use a window after it was closed. The usual practice is this: handle the event Closing or override the� je suis en train de garder une instance de Window autour et en cas de besoin appeler ShowDialog.Cela a fonctionné trouver dans winforms, mais dans WPF je recieve cette exeception:

Error:Cannot set Visibility or call Show or ShowDialog after window , Tips: Window.ShowDialog()方法:Cannot set Visibility or call Show, ShowDialog, 《WPF: Cannot reuse window after it has been closed》. The Closing event is raised when Close is called, if a window's Close button is clicked, or if the user presses ALT+F4. If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window's Closing event is not raised. If the owner of a window is closed (see Owner), Closing is not raised on the owned

Visual Studio / WPF > Form > 閉じたウィンドウを再度ShowModal , Close: When a DockWindow has its CloseAction set to Close, this window is closed and i.e. the DocumentWindow is disposed and you cannot reuse it. Window Height=“Auto” not working as expected ; Is there a way to detect if a browser window is not currently active? WPF: Cannot reuse window after it has been closed ; Open link in new tab or window

关闭Window 之后,无法设置Visibility,也无法调用Show, Reuse XAML elements. An AppWindow lets you have multiple XAML trees with the same UI thread. However, a XAML element can only be added to a XAML tree once. If you want to move a part of your UI from one window to another, you have to manage it's placement in the XAML tree.

Comments
  • Is there a specific reason why you can't instantiate a new one every time? In my opinion it's safer and better anyway.
  • @Alex The root of the problem lies in a thrid party control I'm using. Then gets even more complex when throwing in Prism and Unity. I acutally believe that a singleton form like in the winform days would be easier to implement. When trying the Show/Hide on a non-modal dialog the performance is awesome. However the requirement states that the dialog has to be modal.
  • Does the Show method of the dialog accept a parameter? I found this social.msdn.microsoft.com/Forums/en-US/wpf/thread/… that could maybe help.
  • +1 Just what I was looking for. Surprising change from WinForms to WPF
  • Actually, creating a new window is quite an expensive proposition once you include the cost of all the layout, initialization etc. For somewhat complex windows, this can noticeably improve performance - I've tried it ;-).
  • This is the same answer as Martin Harris just with the code added.
  • In case anyone runs into a similar issue, I had to add Closing="Window_Closing" to XAML in my window tag