多的ViewModels之间的状态共享具体的例子(WPF MVVM) [英] Concrete examples of state sharing between multiple viewmodels (WPF MVVM)

查看:947
本文介绍了多的ViewModels之间的状态共享具体的例子(WPF MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多对象WPF /实体框架(4.0)项目。我想构建应用程序,这样我可以有跨越的ViewModels共享对象的选择状态。

I have a WPF/Entity Framework (4.0) project with many objects. I'd like to build the application so that that I can have object selection state shared across viewmodels.

例如:我们有汽车,司机,乘客及货物类。我们也有卡洛斯,DriverList等用户控件和编辑窗口为CarEditor,DriverEditor等。此外,我们对所有这些的的ViewModels(CarListViewModel,Dri​​verListViewModel,CargoEditorViewModel等)。这一切都构成一个可停靠的接口,用户可以有多个对象列表,编辑器和查看器打开。

For Example: We have Cars, Drivers, Passengers, and Cargo classes. We also have UserControls for CarList, DriverList, etc. and editor windows for CarEditor, DriverEditor, etc. Furthermore, we have viewmodels for all of these (CarListViewModel, DriverListViewModel, CargoEditorViewModel, etc). This all composes a dockable interface where the user can have multiple object lists, editors, and viewers open.

我要的是如何wireup多的ViewModels一个具体的代码示例这样在卡洛斯选择汽车将导致轿厢去住在CarEditorView,而且在对所述上下文是有效的(如DriverByCarView-或只是DriverList如果有一个过滤器谓词)任何其他视图中选择。

What I want is a concrete code example of how to wireup multiple viewmodels so that selecting a car in the CarList will cause that car to go live in the CarEditorView, but also be selected in any other view for which the context is valid (such as a DriverByCarView- or just DriverList if there is a filter predicate).

有许多基于对这个问题的建议和讨论。这两种方法似乎主宰是:

There are a number of suggestions and discussions based on this question. The two methods that seem to dominate are:


  • 3018307 :通过提一个消息子系统讨论状态共享

  • 1159035 :通过使用封闭状态讨论共享视图模型

  • 3018307: Discusses state sharing by mentioning a messaging subsystem
  • 1159035: Discusses state sharing by using an enclosing viewmodel

是这些方法比其他?

有没有人有任何一个具体的例子/都在的形式,这些方法写或小代码项目?

我还在学习WPF中,所以指向切入点阅读API基本面赞赏,但看着代码例子是我经常去。

I'm still learning WPF, so pointers to entry points for reading API fundamentals are appreciated, but looking at code examples is where I usually go.

感谢

在如果任何人有兴趣,这里有其他一些类似的讨论:

In case anyone is interested, here are some other similar discussions:


  • 3816961 :讨论返回根据对象的类型多的ViewModels(即任意类型秉承一个特定的接口)

  • 1928130 :在讨论它是否是一个好主意,总的ViewModels其他的ViewModels的属性(例如一个主窗口视图模型面板的ViewModels组成)

  • 1120061 :在本质上讨论是否有使用视图模型,每-model策略或视图模型,按次元素的策略。

  • 4244222 :讨论是否要巢使用嵌套对象层次时的ViewModels

  • < A HREF =htt​​p://stackoverflow.com/questions/4429708/sharing-data-between-silverlight-viewmodels> 4429708 :讨论分享直接的ViewModels之间的集合,但不细讲

  • 列表项:讨论中的一个管理多个选择单一的视图模型。

  • 3816961: Discusses returning multiple viewmodels depending on object type (i.e. a collection of arbitrary types adhering to a specific interface)
  • 1928130: Discusses whether it is a good idea to aggregate viewmodels as properties of other viewmodels (e.g. a MainWindow viewmodel composed of panel viewmodels)
  • 1120061: Essentially discusses whether to have use a viewmodel-per-model strategy or a viewmodel-per-view-element strategy.
  • 4244222: Discusses whether or not to nest the viewmodels when using a nested object hierarchy.
  • 4429708: Discusses sharing collections between viewmodels directly, but doesn't go into detail.
  • List item: Discusses managing multiple selections within a single viewmodel.

推荐答案

要实现这一目标的典型方法是使用信使发布CarSelected消息,详细说明所选择的车。零个或多个的ViewModels可以订阅CarSelected消息。的ViewModels有兴趣在当前选择的车可以监听消息,然后采取相应的行动。

A typical way to achieve this is to use a messenger to publish a CarSelected message that details the selected car. Zero or more ViewModels can subscribe to the CarSelected message. ViewModels that are interested in the currently selected car can listen for the message and then act accordingly.

使者方法提供了一个干净的分离设计,其中发布者和用户都无依赖性对方这样可以很容易地独立发展 - 他们只需要知道关于汽车的消息。信使调解模式的实现。

The messenger approach provides a clean decoupled design where publishers and subscribers have no dependencies on each other so can easily evolve independently - they just need to know about the car message. Messengers are an implementation of the mediator pattern.

在棱镜使者是 EventAggregator ,用于发布和订阅信息。

In Prism the messenger is the EventAggregator and is used for publishing and subscribing to messages.

更新

除了架构优势的 EventAggregator 带来的,它也实现了微弱的事件,以防止内存泄漏问题与用户没有明确退订。

Apart from the architectural advantages the EventAggregator brings, it also implements weak events to prevent memory leak issues with subscribers that do not explicitly unsubscribe.

请参阅下面的EventAggregator文档:

Please see the following for EventAggregator documentation:

http://msdn.microsoft.com/en-us/library/ff649187.aspx

棱镜:

http://compositewpf.codeplex.com/

棱镜示例

public class ViewModel1
{
    private readonly IEventAggregator _eventService;
    private Car _selectedCar;

    public ViewModel1(IEventAggregator eventService)
    {
        _eventService = eventService;
    }

    //Databound property...
    public Car SelectedCar
    {
        set
        {
            _selectedCar = value;

            var msg = new CarSelectedMessage { Car = _selectedCar };

            _eventService.GetEvent<CarSelectedEvent>().Publish(msg);
        }
    }
}

public class ViewModel2
{
    public ViewModel2(IEventAggregator eventService)
    {
        eventService.GetEvent<CarSelectedEvent>().Subscribe(msg =>
        {
            //DoStuff with msg...
        });
    }
}

public class Car {}

public class CarMessage
{
    public Car Car { get; set; }
}

public class CarSelectedEvent : CompositePresentationEvent<CarMessage> {}

这篇关于多的ViewModels之间的状态共享具体的例子(WPF MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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