问题描述
我如何共享多个的ViewModels之间的数据?结果
How do I share data between multiple ViewModels ?
例如有一个应用程序名为Project类。
For example there is a class named Project in application .
public class Project : ModelBase
{
private string _projectName;
public string ProjectName
{
get { return _projectName; }
set
{
_projectName = value;
RaisePropertyChanged(() => ProjectName);
}
}
}
在多重的ViewModels应用程序应该访问ActiveProject。
结果什么是的ViewModels之间共享项目?
In multiple ViewModels application should access ActiveProject.
What's the best way to share Project between ViewModels ?
- 调解模式的最好方法? (消息)
- 静态对象
- Singleton模式(如果是的话怎么样?)
我用留言之前,但它需要多作弄。对于我已经创建ActiveProject财产和所有的ViewModels必须注册使者更新。
I've used Messaging before but it needs much codding . For all ViewModels I've to create ActiveProject property and also have to register a messenger to update that.
结果
我用MVVM光框架。
结果
的任何代码示例将不胜感激。
I use MVVM Light framework.
Any code example would be appreciated.
推荐答案
我想创建一个作为视图模型作为父母所有项目的ViewModels。 (我们称之为解决方案)
I would create a ViewModel that acts as a parent to all the Project ViewModels. (Let's call it Solution)
解决方案视图模型将有物业ActiveProject和项目的可观察集合。
The Solution ViewModel would have the property ActiveProject and an observable collection of Projects.
这篇关于MVVM:的ViewModels之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!