从一个视图模型访问另一个视图模型的属性 [英] Access properties from one view model in another

查看:42
本文介绍了从一个视图模型访问另一个视图模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WPF 应用程序遵循 MVVM 模式.共有三种视图:

My WPF application follows the MVVM pattern. There are three views:

  • 主窗口
    • 登录查看
    • 项目视图

    LoginViewProjectsView 是由 MainWindow 导入的用户控件.两个视图都分配了视图模型.LoginViewModel 定义了一个属性 ProjectList,它是通过调用网络服务来设置的.现在 LoginViewModel 需要访问 ProjectList 属性和其他属性.

    LoginView and ProjectsView are user controls imported by the MainWindow. Both views have their view model assigned. LoginViewModel defines a property ProjectList which is set by calling a webservice. Now LoginViewModel needs access to the ProjectList property and others.

    我知道一种解决方案可能是重新设计,以便只有一个视图和一个视图模型.我会这样做作为备用解决方案,但我宁愿不这样做.

    I am aware that one solution might be a redesign so that there is only one view and one view model. I would do that as a backup solution but I would favor not to do so.

    这应该怎么做?我应该像 Prism 一样使用某种 EventAggregator 吗?或者还有其他方法可以做到这一点?

    How should this be done? Should I use some kind of EventAggregator like in Prism? Or are there other ways to do this?

    推荐答案

    所以,如果我理解清楚,ProjectList 属性应该可以从LoginViewModel"和ProjectsViewModel"访问.我会尝试在MainViewModel"中实现它,以便子视图模型可以以自然的方式访问它.

    So if i understood clearly, ProjectList property should be accessed from both 'LoginViewModel' and 'ProjectsViewModel'. I'd try to implement it in the 'MainViewModel' so child viewmodels can access it in a natural way.

    IEventAggregator 就像一个盒子,您可以在其中添加事件,或者查找和订阅事件,所以我会说这不是您所需要的.无论如何,您可以在 UnitySingleton.Container 中注册您的自定义接口(框类型),这将公开 ProjectList 以便它可以在任何地方访问.当作为独立程序集的模块需要相互通信时,这种方法很有意义.如果在您的情况下这是否矫枉过正是您应该决定的事情,我个人会选择将其放入 mainviewmodel"选项.

    An IEventAggregator is like a box in which you can add events, or find and subscribe to one, so i would say it's not what you need. Anyway, you could register your custom interface (box type) in the UnitySingleton.Container, which would expose ProjectList for it to be accessible everywhere. This approach makes a lot of sense when modules, which are separate assemblies, need to communicate whith each other. If this is overkill or not in your case is something you should decide, i'd personally go with the 'put it in the mainviewmodel' option.

    -- 样品 --(未测试)

    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            LoginVM = new LoginViewModel(this);
            ProjectsVM = new ProjectsViewModel(this);
            RetrieveProjectList();
        }
    
        public LoginViewModel LoginVM { get; private set; }
    
        public ProjectsViewModel ProjectsVM { get; private set; }
    
        public object ProjectList { get; private set; }
    
        private void RetrieveProjectList()
        {
            ProjectList = ....
        }
    }
    

    正如您所见,这非常简单,LoginVMProjectsVM 将持有对创建它们的 MainViewModel 的引用,因此它们可以访问ProjectList.

    It's pretty simple as you see, LoginVM and ProjectsVM will hold a reference to the MainViewModel that created them, therefore giving them access to ProjectList.

    这篇关于从一个视图模型访问另一个视图模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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