How can I close the first window when the second is active? c# wpf

12 Views Asked by At

I need your help. My project is currently based on two windows, one login and the other for other things. All commands for buttons, login, reset are made in a class "LoginViewModel" using MVVM. How can I close the first window and run the second without problems?

LoginViewModel.CS

public class LoginViewModel : ViewModelBase
{
    #region Login Credentials
    public string Username { get; set; }
    public string Password { get; set; }

    #endregion

    #region Commands

    public ICommand LoginCommand { get; }
    public ICommand ResetPasswordCommand { get; }

    #endregion

    public LoginViewModel()
    {
        LoginCommand = new RelayCommand(_ => Login());
        ResetPasswordCommand = new RelayCommand(_ => Reset());
    }

    #region Private Methods

    private void Login()
    {
        if (Username == "student" && Password == "password")
        {
            AppMainWindow nw = new AppMainWindow();
            nw.Show();
            if(nw.IsActive)
            {
                MainWindow cl = new MainWindow();
                cl.Close();
            }

        } else {
           MessageBox.Show("Username or password is incorect!");
        }
    }
    private void Reset()
    {
        throw   new NotImplementedException();
    }
    #endregion
}

}

First window is: MainWindow Another Window is: AppMainWindow

0

There are 0 best solutions below