ModelView 到 ModelView 通信 [英] ModelView to ModelView communication

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

问题描述

请问,对于以下情况,您建议的最佳(就架构质量而言)方法是什么:

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

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

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

ModelViewB (child) - 保存项目的集合.该集合在概念上属于 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 Light 工具包),我让 ModelViews 相互通知更改.但是,这感觉很尴尬,因为我保留了重复的集合并对其进行了同步.我宁愿只在一个地方使用它并从 ModelViewA 和 B 访问它.我在想也许将一个 ModelView 注入另一个 ModelView 但这会增加耦合并且通常感觉 MVVM 模式是错误的.我也可以只使用对两个 ModelView 的静态引用,因为我有静态定位器(也来自 MVVM Light 工具包)保存引用.或者也许有更好的解决方案?

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?

谢谢,

推荐答案

我现在(有点)正在做这个.

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

我有一个 ConsoleViewModel,它记录和存储来自执行的事件,以便在我的 ConsoleView 的 UI 中显示.自然地,我的 ViewModel 想要与这个控制台通信以记录他们的事件.

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.

为了做到这一点,我创建了一个接口,该接口公开了我的 ViewModel 可以用来将其事件写入控制台的方法.

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 实现了这个接口,所有其他的 ViewModel 都公开了一个 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.

在此过程中,您可以使用多种方法来合成您的 ViewModel.您可以使用 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>

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

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