Singleton standard

Asked

Viewed 448 times

1

I am creating a project in C# and WPF, and I need to control instances of WPF windows, IE if I open a window in a code part with wndJanela.Show(), I want that when calling the Show() method again from another part of the code, open the same first window. For this I used the Singleton standard. But it turns out, when I call wnsJanela.Close() and try again to call the Show(), there is an error in runtime with the following message:

An unhandled Exception of type 'System.Invalidoperationexception' occurred in Presentationframework.dll Additional information: It will not be possible to set Visibility or call Show, Showdialog or Windowinterophelper.Ensurehandle after a Window is closed.

Follow 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;
    }
  • I saw an example of Pattern Singleton implementation at this link: http://www.linhadecodigo.com.br/artigo/3397/singleton-padrao-de-projeto-com-microsoft-net-c-sharp.aspx

1 answer

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.