根据子视图模型更改 wpf 单页应用程序中的视图 [英] Change view in wpf single page application depending upon child view model

查看:38
本文介绍了根据子视图模型更改 wpf 单页应用程序中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 wpf 和 mvvm 模式编写了一个应用程序.我尝试使用 ContentControl 和 DataTemplate 实现单页应用程序.在我的应用程序中,我想根据子视图模型切换视图,如下所示:我有一个带有内容控件的主窗口,它的内容绑定到 MainViewModel 中的一个属性:

I write an application in wpf and using mvvm pattern. I try to implement a single page application using a ContentControl and DataTemplate. In my application I want to switch a view depending on the child view model like this: I have a mainwindow with a content control that its content is binding to a property in MainViewModel:

public BaseViewModel CurrentViewModel { get; set; }

在构造函数中我写道:

CurrentViewModel = new LoginViewModel();

在 LoginViewModel 中,我有一个函数可以获取名称和密码并检查详细信息是否正确.如果可以,我想将 MainViewModel 中的 CurrentViewModel 设置为 NavigationViewModel.

in LoginViewModel I have a function that gets name and password and check if the details are correct. if it's ok, I want to set CurrentViewModel in MainViewModel to NavigationViewModel.

但是当我查找示例时,我发现只能直接在主视图模型中切换视图模型.我该怎么办???

but when I look for examples I find only switch the viewmodel directly in the main view model. how can I do it???

推荐答案

如果我正确理解您的问题,您是在询问如何在 LoginViewModel 之后更改 CurrentViewModel登录完成?

If I understand your question correctly, you're asking how to change CurrentViewModel from within LoginViewModel after login completes?

你不应该这样做.LoginViewModel 应该担心登录,并且不应该知道它的使用位置或应用程序的任何其他部分.

You shouldn't be doing this. LoginViewModel should worry about logging in, and should not know anything about where it's being used, or any other part of the app.

MainViewModel 是拥有子虚拟机并协调应用程序流的那个,所以应该由 MainViewModel 进行切换.

MainViewModel is the one that owns the child VM and orchestrates the flow of the application, so it is MainViewModel that should be doing the switching.

因为你想在登录完成后切换视图,所以你需要LoginViewModel来告诉你登录完成了.这样做的两种明显方法是:

Since you want to switch view after login completes OK, you need LoginViewModel to tell you that login has completed ok. The two obvious ways of doing that are:

  1. LoginViewModel 公开一个 LoginComplete 事件,MainViewModel 订阅,或
  2. MainViewModelLoginViewModel 构造函数提供了一个 LoginComplete ActionLoginViewModel登录完成后调用.
  1. LoginViewModel exposes a LoginComplete event, which MainViewModel subscribes to, or
  2. MainViewModel supplies a LoginComplete Action to the LoginViewModel constructor, which LoginViewModel calls when login is complete.

.

public class MainViewModel 
{
    //INPC omitted for brevity
    public object CurrentViewModel { get; private set; }

    public void MainViewModel()
    {
        this.CurrentViewModel = new LoginViewModel(LoginComplete);
    }

    private void LoginComplete()
    {
        this.CurrentViewModel = new NavigationViewModel();
    }
}

public class LoginViewModel 
{
    private Action loginCompleteAction;

    public void LoginViewModel(Action loginCompleteAction)
    {
        this.loginCompleteAction = loginCompleteAction;
    }

    private void UserHasLoggedIn()
    {
        this.loginCompleteAction();
    }
}

这篇关于根据子视图模型更改 wpf 单页应用程序中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆