无法设置可见性或调用Show,ShowDialog的或EnsureHandle一个窗口关闭后 [英] Cannot set visibility or call Show, ShowDialog or EnsureHandle after a window has been closed

查看:2245
本文介绍了无法设置可见性或调用Show,ShowDialog的或EnsureHandle一个窗口关闭后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的应用程序构造我的WPF应用程序:

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();
        }
    }



在它启动这些应用程序的构造,并通过登录构造云只是罚款,但一旦完成登录构造函数之后再次到达应用程序构造,它与一个InvalidOperationException崩溃,附加信息:不能设置可见或调用Show,ShowDialog的,或WindowInteropHelper.EnsureHandle窗口关闭后

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.

构造的目标是:当应用程序第一次开始,我要检查是否有此应用程序的现有设置如果存在,我想用这些设置连接到第三方(动态CRM 2011),打开应用程序主窗口,然后关闭登录屏幕。如果他们不存在,我希望用户来进行设置。

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.

我已经设法得到它使用2建设者的工作,但ReSharper的抱怨时,我不会,因为我基本上忽略的参数在第二构造(一个我从按钮启动主屏幕上。我想有1统一的构造函数,以便ReSharper的不抱怨。 ?这是可能的。

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?

编辑:我不想让我的登录窗口,因为我创建一个新的窗口,当我更改设置,在我的主窗口使用下面的代码

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. 在启动时,启动Login.xaml;当Login.xaml启动

  2. ,检查设置是否已经被设置;

  3. 如果没有设置,显示Login.Xaml设置。

  4. 如果设置中设置与保存的设置启动主窗口

  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.

另外,我有一个主窗口按钮,有强制启动Login.xaml但不检查是否有设置。这些都是目前独立的构造,我想作1构造它们。

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.

推荐答案

您更新使得它更清楚一点什么就是你想达到的。我建议重组登录窗口,使之更加单一职责,推动了验证逻辑上放入应用类,以便它负责管理初始化流程。配方如下:

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:

阿尔特 App.Xaml.cs ,以便它看起来是这样的;重要的是没有的StartupUri

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>



其中, myNameSpace对象是命名空间的应用类。

现在你要手动启动从 App.OnStartup <应用/ code>

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.
    }
}

要总结:从<$删除您的验证逻辑C $ C>登录窗口应用,只显示登录如果需要的话,只显示主窗口设置是否真正有效。

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.

这篇关于无法设置可见性或调用Show,ShowDialog的或EnsureHandle一个窗口关闭后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆