代码之家  ›  专栏  ›  技术社区  ›  nuuse

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

  •  0
  • nuuse  · 技术社区  · 7 年前

    当我在app.xaml.cs中动态创建主窗口时,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
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   nuuse    7 年前

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

    Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    
    推荐文章