从模块到主Shell数据上下文的Prism和MVVM数据绑定 [英] Prism and MVVM databinding from modules to main shell datacontext

查看:129
本文介绍了从模块到主Shell数据上下文的Prism和MVVM数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF和数据绑定方面有一些经验,但是对于PRISM和MVVM来说是全新的.

我正在开发具有一个外壳和多个模块的Prism应用程序.

在我以前的WPF应用程序中,我有一个窗口数据上下文(包含我需要的所有对象),然后可以从窗口内的任何用户控件进行数据绑定.

在Prism上下文中,拥有单个数据上下文的正确方法是什么,我们将其称为ShellViewModel,然后让所有模块绑定到其对象和属性?因此,如果ShellViewModel中的一个属性发生变化是由一个模块引起的,那么另一个模块可以通过数据绑定检测到该变化,然后触发样式?

也许有一种简单的方法可以做到这一点,但是我对PRISM和MVVM还是陌生的,并且完全困惑我们如何正确进行绑定,尤其是在涉及多个模块的情况下?

任何源代码和示例都很棒.

解决方案

如果您只有一个数据源,请将其作为服务提供给所有视图模型.

将其注册为单例,以便所有视图模型都获得相同的实例.如果您需要该服务将更新推送到视图模型,请使其实现INotifyPropertyChanged并让视图模型观察(最好通过 解决方案

If you have a single source of data, make it available to all your view models as a service.

Register it as a singleton, so that all view models get the same instance. If you need the service to push updates to the view models, make it implement INotifyPropertyChanged and let the view models observe that (done best through a PropertyObserver).

Remember that the view model is the data context of the view, and that it should only communicate data and events between the view and the data source a.k.a. model, but should not own data itself.

这篇关于从模块到主Shell数据上下文的Prism和MVVM数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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