How do I switch views from within a view?

2019-02-02 16:53发布

I'm making a WPF application making use of the MVVM light framework.

What I'm trying to do is have a login form in a view, when the user presses a button within that view it launches a LoginCommand for the attached ViewModel. From there I either want to launch a new window which holds the rest of the application, or simply switch views from the same window.

Currently I have it so that there is a view called MainView which has a content control inside bound to View1. However to switch to View2 I need to put the button for this on MainView, and not within View1 where it belongs.

Any advice?

2条回答
不美不萌又怎样
2楼-- · 2019-02-02 17:35

Usually I do this one of two ways:

If the login window is a one-time thing required before starting the application, I will put it in the OnStartup() method of the Application object

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Login
    var login = new LoginDialog();
    var loginVm = new LoginViewModel();

    login.DataContext = loginVm;
    login.ShowDialog();

    if (!login.DialogResult.GetValueOrDefault())
    {
        // Error is handled in login class, not here
        Environment.Exit(0);
    }

    // If login is successful, show main application
    var app = new ShellView();
    var appModel = new ShellViewModel();

    app.DataContext = viewModel;
    app.Show();
}

The other way I usually do this is through a ShellViewModel or ApplicationViewModel which handles all my window management. This method uses DataTemplates to define each screen, and uses a ContentControl as a placeholder for the current screen in the ShellView or ApplicationView.

I usually combine this with an Event System of some kind, like Microsoft Prism's EventAggregator, so it can listen for messages of a specific type, such as OpenWindow or CloseWindow messages. If you're interested, I have a blog post about Communication between ViewModels that should give you a better idea of what an event system looks like.

For example, my ShellViewModel might start by displaying a LoginViewModel (a DataTemplate is used to tell WPF to draw the LoginViewModel with the LoginView), and it would subscribe to receive messages of type SuccessfulLogin. Once the LoginViewModel broadcasts a SuccessfulLogin message, the ShellViewModel will close the LoginViewModel and replace it with the ApplicationViewModel. You can see an example of this in my article on Navigation with MVVM

查看更多
贼婆χ
3楼-- · 2019-02-02 17:35

Put your views inside Page elements, inside your MainWindow create a Frame and point it's source to your first page.

From then on you can use the frame's NavigationService to navigate your frame to another view, much like a web browser.

查看更多
登录 后发表回答