Windows 在 App.xaml.cs 中创建后不要打开

Windows don't open after creation in App.xaml.cs

当我在 app.xaml.cs 中动态创建 MainWindow 时,mainWindow.Show() 函数将被忽略。每个 Xaml 属性,如 window.height 等都设置为 'NaN'.
其他每个 Window 也是如此,即使我创建一个新的 xaml.

奇怪的是,如果我在我的 while 循环之前打开任何 Window,例如 Window1,并显示它,while 循环之后的所有 windows 也会打开。

因此,我可以做一个解决方法,并初始化一个 window,它会立即关闭。但如前所述,这只是一种解决方法。

谁能回忆起这种奇怪的行为?

private void App_OnStartup(object sender, StartupEventArgs e)
    {
        var win = new Window1();  // if I comment out these two lines,
        win.Show();               // no Windows, except the 'Einloggen' window will be shown

        var i = 0;
        while (i < 3) // 3 mal falsch = Programm wird beendet
        {
            var login = new Einloggen();
            login.ShowDialog(); //ShowDialog is intentional, I want to wait until the user has entered his credentials, then 'login' closes automatically
            if (login.Anmeldeverusch)
            {
                if (login.TBUsername.Text != string.Empty && login.PBPasswort.Password != string.Empty)
                {
                    var log = Login(login.TBUsername.Text, login.PBPasswort.Password); //The Login(string, string) method opens a connection to the Database. If that does not throw an exception, the user is authorized to use the Program, and the method will return True
                    if (log)
                    {
                        break;
                    }
                }

                i++;
            }
            else
            {
                Environment.Exit(1);
            }
        }

        if (i > 2)
        {
            MessageBox.Show("Drei fehlgeschlagene Anmeldeversuche!");
            Environment.Exit(0);
        }

        var mainWindow = new MainWindow();
        mainWindow.Show(); // .Show and .ShowDialog() will be ignored. they dont throw an excetption. The entire program just closes after the my App_OnStartup is over
    }

我的问题已通过以下行解决,在打开任何 window:

之前设置
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;