开发者

MVVM - how show view?

开发者 https://www.devze.com 2023-01-12 05:36 出处:网络
My MVVM application started with App.xaml.cs Here I create a main window. It has frame. Here I put LoginView.

My MVVM application started with App.xaml.cs

Here I create a main window. It has frame. Here I put LoginView.

It has button "Login". I have command, which checks and do login.

This code I have in LoginViewModel. If all ok - I should show the next View. How I can do it?

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 I should show all necessary views? I Use now WPF MVVM Toolkit.


In a situation such as this you could have your startup form be your main program, and the Login is a dialog box. If the dialog box fails, exit the program. If it succeeds, proceed in loading up the main form.

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

}


I don't know anything about the MVVM Toolkit, but a simple way I did this was to have a delegate to do it, something like: (simplified code)

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

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

    // ...
} 

So you have a 'ShowNextScreen' action on your VM which calls this code.

I have a small app on Google Code which does that (it also deals with only having one form open, error handling etc). Note that in this case, it's the ViewModel which has responsibility for opening the view.

But this is going to get complicated quickly, for any reasonable size of app you'd want to split this functionality out into some kind of 'application controller' which dealt with opening screens, navigation etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号