简体   繁体   English

多个ViewModel之间的数据共享

[英]Data sharing between multiple ViewModels

Further to my question how can I bind Bing Pushpins from multiple models? 继我的问题如何从多个模型绑定Bing Pushpins?

This is pretty new to me and I have been searching through the web but there seem so many different approaches to MVVM and then adding in WP7 and I have got a bit confused 这对我来说是一个新手,我一直在网上搜索,但似乎有很多不同的MVVM方法,然后在WP7中添加,我有点困惑

I am now trying to work out the best way to share data between ViewModels or even if that is the best way to do it. 我现在正在尝试找出在ViewModel之间共享数据的最佳方法,即使这是最好的方法。

What I mean is I have, for example 我的意思是我有,例如

My models: PeopleModel, BuildingModel My ViewModels: PeopleViewModel, BuildingViewModel (which contain Observable collections of the model) 我的模型:PeopleModel,BuildingModel My ViewModels:PeopleViewModel,BuildingViewModel(包含模型的Observable集合)

At the moment a Timer is used to update the lists from a Webservice. 目前,Timer用于从Web服务更新列表。 The ViewModel because it is static is able to be updated during the lifetime of the application. ViewModel因为它是静态的,所以能够在应用程序的生命周期内更新。 I am not sure this is the most sensible approach though but I need some form of background sync to fit the requirements. 我不确定这是最明智的方法,但我需要某种形式的背景同步来满足要求。

The People and Building contain a location but not anything regarding what image it should display as a pushpin. 人物和建筑物包含一个位置,但没有任何关于它应该显示为图钉的图像。 So I was thinking if I had a my map view containing a MapViewModel that is linked somehow to the ViewModels but I am not sure how you would do this. 所以我在想我是否有一个包含MapViewModel的地图视图,该视图以某种方式链接到ViewModel但我不知道你会怎么做。

I looked at MVVMLight and it seems you can register the ViewModels at start so it would be possible to add links to the other ViewModels and not worry about the lifetimes of them? 我看了MVVMLight,似乎你可以在开始时注册ViewModel,这样可以添加到其他ViewModel的链接而不用担心它们的生命周期?

However given that there is extra information within the Models that the Map isn't interested in I wonder if is better to have a self-contained MapViewModel that contains lists of Custom pushpins of some type (so PeoplePushpins, BuildingPushpins). 然而,鉴于模型中存在地图不感兴趣的额外信息,我想知道是否更好地拥有一个包含某种类型的自定义图钉列表的自包含MapViewModel(所以PeoplePushpins,BuildingPushpins)。 If I go this route I would like to know how you update the MapViewModel from data updated in the other Models. 如果我走这条路线,我想知道如何从其他模型中更新的数据更新MapViewModel。

What I mean is the running timer in PersonViewModel detects a change in the list, so updates its own list. 我的意思是PersonViewModel中的运行计时器检测到列表中的更改,因此更新自己的列表。 I the need to send notification to the Map that there is an update which then will update itself from that. 我需要向Map发送通知,告知有更新,然后更新自己。

Any help/advice gratefully received. 感谢任何帮助/建议。

With MVVMLight you can use messaging to send data between models: 使用MVVMLight,您可以使用消息传递在模型之间发送数据:

//build class to send as message  
public class AddPushPinMessage
{
    public PushPin PushPin { get; set; }
}

public class ReceivingViewModel
{
  public ReceivingViewModel()
  {
     Messenger.Default.Register<AddPushPinMessage>(this, (m) => AddPushPin(m));
  }

  void AddPushPin(AddPushPinMessage msg)
  {
     //handle message
  }
}

public class SendingViewModel
{
  private object SendPushPin(PushPin key)
  { 
    Messenger.Default.Send<AddPushPinMessage>(new SetPushPinMessage() { PushPin = key });

    return null;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM