模型视图到模型视图的通信

发布于 2024-11-24 05:16:38 字数 529 浏览 2 评论 0原文

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

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

ModelViewB (子级) -持有物品的集合。该集合在概念上属于 ModelViewB,并且主要在此(模型)视图内进行修改。但是,ModelViewA 也可以修改集合

。我当前采用的方法是在 ModelViewB 中拥有集合,并在 ModelViewA 中拥有副本。使用消息总线(MMVM Light 工具包),我让 ModelView 相互通知更改。然而,这感觉很尴尬,因为我保留了重复的集合并同步它。我宁愿只将它放在一个地方并从 ModelViewA 和 B 访问它。 我在想也许将一个 ModelView 注入到另一个 ModelView 中,但这会增加耦合,并且通常感觉 MVVM 模式是错误的。我也可以只使用对两个 ModelView 的静态引用,因为我有静态定位器(也来自 MVVM Light 工具包)保存引用。 或者也许有更好的解决方案?

谢谢,

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

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

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

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?

Thanks,

O

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-12-01 05:16:38

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

我有一个 ConsoleViewModel,它记录和存储执行中的事件,以便在 ConsoleView 的 UI 中显示。当然,我的 ViewModel 希望与此控制台通信以记录它们的事件。

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

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

我的 ConsoleViewModel 实现了此接口,并且所有其他 ViewModel 都公开了 IConsole 类型的公共属性,它们使用该属性写入控制台。

在此过程中,您可以使用多种方法来组合 ViewModel。您可以使用 DI 或简单的服务定位器,或者(正如我所做的那样)在资源中定义它们。

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

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

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.

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
}

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文