Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Close Login window and open mainwindow on Login

$
0
0
Hi,
I have a WPF app using MVVM with a login screen, on login, I need the login window to close and the Mainwindow to open.
Here's what I'm trying to do to achieve that:
In App.xaml.cs:
 protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var login = new Login();
            var loginVM = new LoginViewModel();
            loginVM.LoginCompleted += (sender, args) =>
                {
                    MainWindowViewModel mvm = new MainWindowViewModel();
                    MainWindow main = new MainWindow();
                    main.DataContext = mvm;
                    main.ShowDialog();
                  //tried main.Show() as well
                };

            login.DataContext = loginVM;
            login.ShowDialog();
        }
And in the Loginviewmodel.cs
 public void Login()
        {
            if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
            {
               //code to authenticate user
               ....

                //Call to the event
                RaiseLoginCompletedEvent();
              
            }
        }

        public event EventHandler LoginCompleted;
        private void RaiseLoginCompletedEvent()
        {
            var handler = LoginCompleted;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

The mainwindow opens fine after clicking the login button, but the login window doesn't close. It remains open too.What am I doing wrong here? I'm still learning WPF & MVVM.

Thank you

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>