WPF/MVVM

编程入门 行业动态 更新时间:2024-10-27 05:30:01
本文介绍了WPF/MVVM - 将 ViewModels 保留在应用程序组件中还是分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 WPF 应用程序,我在其中使用了 MVVM 模式.

I have a WPF application where I'm using the MVVM pattern.

我为需要用户输入的操作激活了 VM,因此需要从 VM 激活视图.

I get the VM activated for actions that require user input and thus need to activate views from the VM.

我开始将 VM 分成单独的组件/程序集,部分原因是我将它们视为单元可测试部分,部分原因是视图应该依赖于 VM,而不是相反.但是当我需要打开一个窗口时,VM 不知道该窗口.

I've started out separating the VMs into a separate component/assembly, partly because I see them as the unit testable part, partly because views should rely on VM, not the other way round. But when I then need to bring up a window, the window is not known by the VM.

我发现的所有介绍都将 VM 放在 WPF/App 组件中,从而消除了问题.

All introductions I find place the VM in the WPF/App component, thus eliminating the problem.

本文建议将它们放在单独的层中:waf.codeplex/wikipage?title=Architecture%20-%20Get%20The%20Big%20Picture&referringTitle=Home

This article recommends keeping them in separate layers : waf.codeplex/wikipage?title=Architecture%20-%20Get%20The%20Big%20Picture&referringTitle=Home

据我所知,我有以下选择

As I see it, I have the following choices

  • 将虚拟机移动到 WPF/App 程序集以允许虚拟机直接访问窗口.

  • Move VMs to the WPF/App assembly to allow VMs to access the windows directly.

    将视图的接口放在VM-assembly中,在WPF/App assembly中实现视图,并通过IOC或其他方式注册连接.

    Place interfaces of views in VM-assembly, implement views in WPF/App assembly and register the connection through IOC or alternative ways.

    将来自 VM 的请求"提交到路由请求的某种机制/总线(但哪种机制!?例如 Prism 中的某些东西?!)

    File a 'request' from the VM into some mechanism/bus that routes the request (but which mechanism!? E.g something in Prism?!)

    有什么建议?

    感谢您的任何评论,

    丹麦安德斯

    推荐答案

    不要选择选项 1.您将添加一个不需要的从 VM 到 V 的依赖项.

    Don't pick option 1. You'll be adding an unwanted dependency from VM to V.

    选项 2 和 3 都有效并且正在使用.在这些之间进行选择有时是一个品味问题.我认为 IOC 启用/允许模拟更好,而消息总线适用于小型应用程序.

    Options 2 and 3 are both valid and being used. Picking between these is sometimes a matter of taste. I think that IOC enables/allows mocking better whereas a messagebus works fine for small applications.

  • 更多推荐

    WPF/MVVM

    本文发布于:2023-11-13 14:10:54,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1584530.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:WPF   MVVM

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!