WPF - choose startup window based on some condition

前端 未结 4 1174
傲寒
傲寒 2020-12-08 09:53

When running my program by clicking Run or pressing Ctrl + F5, is it possible to open different windows based on some check condition?

I.e

4条回答
  •  生来不讨喜
    2020-12-08 10:27

    look into App.xaml

    remove StartupUri="MainWindow.xaml"

    add Startup="Application_Startup" new event Handler

    
    

    form code behind App.xaml.cs create Application_Startup like...

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //add some bootstrap or startup logic 
            var identity = AuthService.Login();
            if (identity == null)
            {
                LoginWindow login = new LoginWindow();
                login.Show();
            }
            else
            {
                MainWindow mainView = new MainWindow();
                mainView.Show();
            }
        }
    

提交回复
热议问题