Singleton Pattern

1

I'm creating a project in C # and WPF, and I need to control instances of WPF windows, that is, if I want to open a window in a part of code with wndJanela.Show() , I want it when I call the Show () method again. another part of the code, open the same first window. For this I used the Singleton standard. But it happens that when I call wnsJanela.Close() and try again to call Show (), there is a run-time error with the following message:

  An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll   Additional information: You can not set Visibility nor call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window is closed.

Here is my code below:

public static wndCadastroUsuario instance; //Campo publico estático que armazena a instancia da classe
    private wndCadastroUsuario() //Construtor privado
    {
        InitializeComponent();
    }

    public static wndCadastroUsuario getInstance() //Método para obter a instancia
    {
        if (instance == null)
        {
            instance = new wndCadastroUsuario();
        }

        return instance;
    }
    
asked by anonymous 10.06.2016 / 05:36

1 answer

1

If you want to reopen the window at some point, then do not use Close , use the method Hide

    
10.06.2016 / 13:34