在ViewModel之间共享收藏 [英] Sharing Collection between ViewModels

查看:141
本文介绍了在ViewModel之间共享收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过,没有什么可以帮助我到达需要的地方。

I've searched and nothing is helping me get to where I need to get.

我的问题的描述:


  1. 我们有一个对话框窗口,其中包含多个视图(用户控件)。单击下一步,将使您在该对话框中向前和向后移动,类似于向导。

  1. We have a single dialog window which hosts multiple views (usercontrols). Clicking next and back will move you forward and backwards in this dialog similar to a wizard.

在6个对话框中,其中4个引用相同的核心数据。例如,我们将说一个ObservableCollection

Out of the 6 dialogs, 4 of them reference the same core data. For example we will say an ObservableCollection

这种情况,我有4个viewModel,它们都需要引用相同的ObservableCollection。我不想保存并在每次进入向导对话框的新步骤时重新加载列表。

That being the case, I have 4 viewModels which all need to reference this same ObservableCollection. I don't want to save and then reload the list everytime I move on to a new step in the "wizard" dialog.

我的问题是实现这一目标的最佳/最实用方法是什么?

My question is what's the best/most practical way to accomplish this.

我考虑过以下事情:


  1. 静态类

  2. Singleton-ehhhh

  3. 在视图之间传递参数(尽管这很困难,因为nextlocation和previouslocation非常通用)。

  4. 介体模式?我对调解员模式的问题是,我不想在视图之间进行交流。我只希望所有视图共享相同的数据源。

  5. 观察者模式?如果我使用的是ObservableCollections并实现了INotifyPropertyChanged,那么我不需要通知任何更改吗?

  1. Static class
  2. Singleton - ehhhh
  3. Passing parameters between views (although this is difficult as the nextlocation and previouslocation are very generic).
  4. Mediator pattern? My problem with the mediator pattern is that I don't want to "communicate" between views. I just want all of the views to share the same data source.
  5. Observer pattern? If I'm using an ObservableCollections and implementing INotifyPropertyChanged, then I shouldn't need to notify anyone of a change should I?

请让我知道您可能需要帮助的其他信息,我会很乐意提供。

Please let me know additional info you might need to help me with this and I'll happily provide it.

我并不是真正在寻找设计示例的代码示例。虽然如果代码示例可以帮助解释设计,但我全力以赴。

I'm not really looking for code examples as I am design. Although if code examples can help explain the design, I'm all for it.

最后,EventAggregator不是一个选择,因为我不使用任何框架(除非我'我没有正确理解EventAggregator)。

Lastly, EventAggregator is not an option as I'm not using any frameworks (unless I'm not understanding EventAggregator correctly).

预先感谢!

推荐答案

像兰道夫(Randolf)所说,直接投资将行之有效。我使用MEF和共享的CreationPolicy进行构造器注入。您只需要将要为4views处理的内容放在一个导出类中即可。多么容易:)

like Randolf said DI would work. i do this with MEF and contructor injection with CreationPolicy shared. you just have to put the stuff you wanna handle for you 4views in one export class. how easy is that :)

和顺便说一句:中介模式是视图模型之间的交流而不是视图之间的联系。

and btw: Mediator pattern is "communicate" between viewmodels not views.

[Export]
public class MyClassWichHasDataSharedFor4Views {}

public class Viewmodel1
{
   [ImportingContructor] 
   public Viewmodel1(MyClassWichHasDataSharedFor4Views shareddata)
   {}
}

public class Viewmodel2
{
   [ImportingContructor] 
   public Viewmodel2(MyClassWichHasDataSharedFor4Views shareddata)
   {}
}

public class Viewmodel3
{
   [ImportingContructor] 
   public Viewmodel3(MyClassWichHasDataSharedFor4Views shareddata)
   {}
}

public class Viewmodel4
{
   [ImportingContructor] 
   public Viewmodel4(MyClassWichHasDataSharedFor4Views shareddata)
   {}
}

现在4个视图模型都引用了您的核心数据

now the 4 viewmodels has a reference to your core data

这篇关于在ViewModel之间共享收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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