C# Windows不支持';在App.xaml.cs中创建后无法打开

C# Windows不支持';在App.xaml.cs中创建后无法打开,c#,wpf,app.xaml,C#,Wpf,App.xaml,当我在app.xaml.cs中动态创建MainWindow时,MainWindow.Show()函数将被忽略。每个Xaml属性(如window.height等)都设置为“NaN”。 其他窗口也是如此,即使我创建了一个带有空xaml的新窗口 奇怪的是,如果我打开while循环之前的任何窗口,比如Window1,并显示它,while循环之后的所有窗口也会打开 因此,我可以做一个变通方法,初始化一个立即关闭的窗口。但正如所说,这只是一个变通办法 有人能回忆起这种奇怪的行为吗 private void

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

奇怪的是,如果我打开while循环之前的任何窗口,比如Window1,并显示它,while循环之后的所有窗口也会打开

因此,我可以做一个变通方法,初始化一个立即关闭的窗口。但正如所说,这只是一个变通办法

有人能回忆起这种奇怪的行为吗

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
    }
private void应用程序启动(对象发送方、StartupEventArgs e)
{
var win=new Window1();//如果我注释掉这两行,
win.Show();//不显示任何窗口,只显示“Einloggen”窗口
var i=0;
而(i<3)//3 mal falsch=wird-beendet程序
{
var login=new-einlogen();
login.ShowDialog();//ShowDialog是故意的,我想等到用户输入了他的凭据后,“login”就会自动关闭
if(login.Anmeldeverusch)
{
if(login.TBUsername.Text!=string.Empty&&login.PBPasswort.Password!=string.Empty)
{
var log=Login(Login.TBUsername.Text,Login.PBPasswort.Password);//Login(string,string)方法打开到数据库的连接。如果这不会引发异常,则用户有权使用该程序,并且该方法将返回True
如果(日志)
{
打破
}
}
i++;
}
其他的
{
环境。出口(1);
}
}
如果(i>2)
{
MessageBox.Show(“dreifehlgeschlageneanmeldeversuche!”);
环境。退出(0);
}
var mainWindow=新的mainWindow();
mainWindow.Show();/.Show和.ShowDialog()将被忽略。它们不会引发异常。整个程序在“我的应用程序”启动后关闭
}

我的问题已通过在打开任何窗口之前设置的以下行解决:

Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

可能尝试不要将
ShowDialog
作为第一个窗口运行。对话框表示有人将等待此窗口关闭。尝试使用
einlogen.Show
instead@Joschka要同时打开所有窗口吗?函数以模态方式调用窗口,这意味着windowA.ShowDialog()之后的代码;在该窗口关闭之前不会执行ShowDialog,因为我希望用户等待我的登录函数返回true,为此用户需要输入其凭据。如果他在“Einloggen”窗体中按Login,窗体将自动关闭。可能尝试创建MainWindow实例,但不打开它,然后调用
Login.Owner=MainWindow
查看示例代码,它可能会显示ShutdownMode属性的重要性。