模型视图为模型视图通信 [英] ModelView to ModelView communication

查看:141
本文介绍了模型视图为模型视图通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,那你的建议是最好的(在建筑质量方面)以下情形的方法:

Please, what do you suggest is the best (in terms of architectural quality) approach to the following scenario:

ModelViewA(父) - 需要从ModelViewB收集来显示数据 - 收集访问多次,即使集合保持不变。

ModelViewA (parent) - requires collection from ModelViewB to display data - access collection multiple times even when the collection remains unchanged

ModelViewB(子) - 持有项目的集合。集合概念上属于ModelViewB这(模型)视图中主要修改。然而,ModelViewA还可以修改该集合

ModelViewB (child) - holds collection of items. The collection conceptually belongs to ModelViewB and is primarily modified within this (model)view. However, ModelViewA can also modify the collection

目前的方法,我采取的是有收藏ModelViewB和ModelViewA重复。使用消息总线(MMVM光工具包)我有ModelViews通知对方的改变。然而,这种感觉别扭,因为我必须不断重复采集和同步。我宁愿有只在一个地方,来自ModelViewA和B.访问
我在想或许注射1模型视图到另一个但是这会增加耦合和一般的感觉错了MVVM模式。我也可以只使用静态引用到两个ModelViews因为我有静态定位器(也来自MVVM光工具包)控股的引用。
或者,也许有一个更好的解决方案?

The current approach I take is having collection in ModelViewB and a duplicate in ModelViewA. Using messaging bus (MMVM Light toolkit) I have the ModelViews notify each other of the change. However, this feels awkward since I have keep the duplicate collection and synchronize it. I would much rather have it only in one place and access it from both ModelViewA and B. I was thinking perhaps injecting one ModelView to another but that would increase coupling and generally feels wrong for MVVM pattern. I could also just use the static reference to both ModelViews since I have the static locator (also from MVVM Light toolkit) holding the references. Or perhaps there is a better solution?

谢谢,

0

推荐答案

我(在某种程度上),现在这样做的。

I'm (sort of) doing this right now.

我有在我的ConsoleView UI记录和存储事件从执行用于显示的ConsoleViewModel。当然,我的ViewModels希望与此控制台进行沟通,以便记录他们的活动。

I have a ConsoleViewModel which records and stores events from execution for display in the UI in my ConsoleView. Naturally, my ViewModels wish to communicate with this console so as to record their events.

在为了做到这一点,我创建了公开方式,我的ViewModels可以用它来写自己的事件控制台的接口。

In order to do this, I created an interface which exposes methods my ViewModels can use to write their events to the console.

public interface IConsole
{
  void Info(string message);
  void Info(string messageFormat, params object[] args)
  // etc
}

我ConsoleViewModel实现了这个接口,和所有其他的ViewModels揭露,他们用它来写控制台类型IConsole的公共属性。

My ConsoleViewModel implements this interface, and all other ViewModels expose a public property of type IConsole which they use to write to the console.

在执行此操作,您可以使用您的合成中的ViewModels许多方法。您可以使用DI,或者一个简单的服务定位器,或(像我这样做),在资源定义它们。

In doing this, you can use many methods of compositing your ViewModels. You can use DI, or a simple service locator, or (as I have done), define them in a resource.

<Application.Resources>
    <ConsoleViewModel x:Key="ConsoleViewModel" />
    <DerpViewModel x:Key="Derp" 
        Console="{StaticResource ConsoleViewModel}" />
    <!--etc-->     
</Application.Resources>

这篇关于模型视图为模型视图通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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