MVVM - how to show view?

My MVVM program is running with App.xaml.cs

Here I create the main window. It has a frame. Here I inserted LoginView.

It has a login button. I have a command that checks and logs in.

I have this code in LoginViewModel. If everything is fine, I must show the following view. How can i do this?

App.xaml.cs

        private void OnStartup(object sender, StartupEventArgs e)
        {
            LoginViewModel loginVM = new LoginViewModel();    
            MainView mainView = new MainView();            
            LoginView loginView = new LoginView();
            loginView.DataContext = loginVM;
            mainView.Frame.Content = loginView;
            mainView.Show();

        }

LoginViewModel.cs

// this method calls by binding after Click Login in LoginView
    private void Login()
        {
            //TODO: Realize it
            if (LoginModel.Login("User1", "Password"))
            {
               // HERE I SHOULD CLOSE LOGINVIEW AND SHOW NEXT VIEW
            }
        }

How and where should I show all the necessary submissions? I am using the WPF MVVM Toolkit.

+3
source share
2 answers

, Login - . , . , .

private void OnStartup(object sender, StartupEventArgs e)
{
    LoginViewModel loginVM = new LoginViewModel(); 
    LoginView loginView = new LoginView();    
    loginView.DataContext = loginVM;  
    loginView.ShowDialog(); // Change this to a ShowDialog instead of Show     

    if (!login.DialogResult.GetValueOrDefault())
    {
        // Should probably handle error in login class, not here");
        Environment.Exit(0);
    }

    // This code will never get reached if Login fails
    MainView mainView = new MainView();   
    mainView.Frame.Content = loginView;
    mainView.Show(); // Change this to a ShowDialog instead of Show

}
+2

MVVM Toolkit, , , , : ( )

private void OnStartup(object sender, StartupEventArgs e) 
{ 
    LoginViewModel loginVM = new LoginViewModel();

    loginVM.ShowNextScreen += () => {
        SomeOtherVM nextVM = new SomeOtherVM();
        nextVM.ShowForm();
    }

    // ...
} 

, "ShowNextScreen" , .

Google Code, ( , ..). , ViewModel, .

, - " ", , ..

0

Source: https://habr.com/ru/post/1760771/


All Articles