当我在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
}
我的问题已经用以下行解决,在打开任何窗口之前设置:
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;