Cannot set visibility or call Show, ShowDialog or EnsureHandle after a window has been closed

14,917

Your update makes it a bit clearer what it is you want to achieve. I suggest restructuring the Login window to make it more single responsibility and pushing the validation logic up into the App class so that it is responsible for managing initialization flow. A recipe is as follows:

Alter App.Xaml.cs so that it looks something like this; importantly there is no StartupUri:

<Application 
    x:Class="MyNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources />
</Application>

Where MyNamespace is the namespace of your App class.

Now you are going to manually start the application from App.OnStartup

public partial class App
{
    protected override void OnStartup(System.Windows.StartupEventArgs e)
    {
        base.OnStartup(e);

        if (!AreSettingsSet())
        {
            this.MainWindow = new Login();
            this.MainWindow.ShowDialog(); // Waits until closed.

            // Recheck the settings now that the login screen has been closed.
            if (!AreSettingsSet())
            {
                // Tell the user there is a problem and quit.
                this.Shutdown();
            }
        }

        this.MainWindow = new MainWindow();
        this.MainWindow.Show();
    }

    private bool AreSettingsSet()
    {
        // Whatever you need to do to decide if the settings are set.
    }
}

To summarise: remove your validation logic from the Login window to App, only show Login if needed and only show MainWindow if the settings are actually valid.

Share:
14,917
Nzall
Author by

Nzall

Updated on June 14, 2022

Comments

  • Nzall
    Nzall about 2 years

    This is my App constructor for my WPF application:

    public partial class App
    {
        public App()
        {
            Run(new Login(false));
        }
    
    
    }
    

    And this is my Login constructor:

    public Login(bool ignoreSettings)
        {
            InitializeComponent();
            if (ignoreSettings)
            {
                TxtUsername.Text = SettingsSaver.LoadUsername();
                TxtCrmUrl.Text = SettingsSaver.LoadCrmUrl();
                return;
            }
            if (string.IsNullOrWhiteSpace(SettingsSaver.LoadUsername()) || string.IsNullOrWhiteSpace(SettingsSaver.LoadCrmUrl())) return;
            try
            {
                CrmConnector.ConnectToCrm();
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
            }
            catch (SecurityAccessDeniedException)
            {
                MessageBox.Show(@"Uw inloggegevens zijn niet correct. Gelieve deze te controleren en opnieuw te proberen.");
            }
            finally
            {
                Close();
            }
        }
    

    It starts the App constructor and goes through the Login constructor just fine, but once it reaches the App Constructor again after finishing the Login constructor, it crashes with an InvalidOperationException, with additional information: "Cannot set visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after the window has been closed.

    The goal of the constructor is as follows: When the application is first started, I want to check if there are existing settings for this application. If they exist, I want to use those settings to connect to a 3rd party (Dynamics CRM 2011), open the main application window, and then close the Login screen. if they are not there, I want the user to set the settings.

    HOWEVER, I also want to be able to start this window from a button on my main screen, in which case it should ignore the default settings and launch the login window again, allowing me to set the settings again.

    I already managed to get it to work using 2 constructors, but Resharper complains when i does that because I basically ignore the parameter in the second constructor (the one which I launch from the button on the main screen. I'm trying to have 1 unified constructor so Resharper does not complain. Is that possible?

    Edit: I don't want to keep my login window because I create a new window when I change the settings, using the following code in my MainWindow:

    private void BtnSettings_Click(object sender, RoutedEventArgs e)
        {
            Login login = new Login(true);
            login.Show();
            Close();
        }
    

    edit: some clarification: I don't want to show multiple windows. What I want is:

    1. on startup, launch Login.xaml;
    2. when Login.xaml is launched, check if the settings have already been set;
    3. if no settings, show Login.Xaml for setting;
    4. if Settings set, start MainWindow with saved settings.

    In addition, I have a button on MainWindow which has to force-start Login.xaml but not check if there are settings. These are currently separate constructors and I would like to make 1 constructor of them.

  • Nzall
    Nzall about 10 years
    I don't really want to keep it, because I need to recreate the main window if I change the settings.
  • Reporter
    Reporter about 10 years
    @Mikael can you add example source code? That would be clearer to understand your answer.
  • Nzall
    Nzall about 10 years
    What do I need the second ShowWindow1() for? Isn't one of those enough?
  • Nzall
    Nzall about 10 years
    I don't want to show multiple windows. what I want is: 1) on startup, launch Login.xaml; 2) when Login.xaml is launched, check if the settings have already been set; 3a) if no settings, show Login.Xaml for setting; 3b) if Settings set, start MainWindow with proper settings. In addition, I have a button on MainWindow which has to force-start Login.xaml but not check if there are settings. These are currently separate constructors and I would like to make 1 constructor of them.
  • Nzall
    Nzall about 10 years
    Also, I've tried to adapt this method and it still fails. I keep getting the error.
  • Sinatr
    Sinatr about 10 years
    You still getting error because of that adapting. Do you create a new instance of login window before displaying it again? I saw you don't want to hide window, but restart. Then you have to create a new instance.
  • Nzall
    Nzall about 10 years
    I create a new instance of the login window when I need to show it after the ButtonSettings_Click. This new window in turn also creates a new MainWindow when the settings are saved again. But it does not crash on that stage, it crashes when I first launch the window. Could this be because of my Telerik controls?
  • Sinatr
    Sinatr about 10 years
    I've no idea, but my answer will ensure what the first instance of window1 is deleted (disposed) when second instance is used to show the same window1.
  • Nzall
    Nzall about 10 years
    but window1 (Login) only needs to be shown on 2 occasions: Either it's called with the parameter true or there are no settings to retrieve. on any other case, it needs to start, see that there are settings to retrieve, start MainWindow with those settings and then close itself.
  • Nzall
    Nzall about 10 years
    I'm currently implementing this. Does this also mean that I can have 1 constructor Login(bool IgnoreSettings) instead of 1 with and 1 without the bool parameter?
  • Nzall
    Nzall about 10 years
    actually, scratch that comment. With this change, I can implement it so that it doesn't need to have an ignoreSettings bool parameter, because when I show it, it always has to ignore the settings.
  • satnhak
    satnhak about 10 years
    Yes that's right: the Login screen now only gets displayed when it needs to: something else decides if it should be shown. That makes the logic more simple.
  • Nzall
    Nzall about 10 years
    True. I did have to add an extra check to verify that I don't call MainWindow() twice when I'm logging in for the first time, because it was called both in OnStartup() and in Loginform.BtnLogin_click(). This means I'll likely have to slightly alter when I call CrmConnector.ConnectToCRM().