用于 Winform 的 MVVM [英] MVVM for winforms

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

问题描述

可能的重复:
Windows 窗体的 UI 设计模式(如 WPF 的 MVVM)

MVVM 应该用于 WinForms 吗?如果是这样,与使用 MVP 相比有什么优势?

Should MVVM be used for WinForms? If so, what is the advantage over using MVP?

推荐答案

我认为这里有两个答案......实际上只有一个答案是我应该",一个是我可以".

I think that there are two answers here... really just one answer to "Should I" and one answer to "Could I".

至于我能不能",这当然是可能的.MVVM 实际上只是依赖于可以绑定到视图模型的视图.由于 WinForms 支持绑定,这当然是可能的.您可能需要编写一些代码来使该绑定在 MVVM 世界中更有用,但这(至少)在理论上是可能的.如果它运作良好,好处将是非常大的,IMO.您可以确保您的 WinForms视图"没有 UI 行为,除了创建可视对象并绑定它们(在代码中,而不是像在 XAML 中那样声明性).WinForms 对象很难测试,而 ViewModel 很容易测试.

As far as "Could I", it is certainly possible. MVVM really just relies on a view that can bind to a view model. Since WinForms supports binding, this certainly is possible. You may need to write some code to make that binding more useful in an MVVM world, but it is (at least) theoretically possible. If it worked well, the benefits would be pretty great, IMO. You could make sure that your WinForms "View" had no UI behavior, except for creating the visual objects and binding them (in code, not declarative like in XAML). WinForms objects are very difficult to test, where ViewModels are very easy to test.

至于你真正的问题:我应该",这更像是一个项目级别的决定.你的目标是什么?如果您希望使一些相当复杂的 UI 逻辑可测试,那么您至少可以研究一下.不过,幸运的是,还有其他模式(例如模型-视图-演示者)拥有更多社区支持,也可以让您编写可测试的演示者"类.我发现与 Presenters 相比,ViewModels 更容易编写单元测试,但我认为这是个人偏好.

As far as your real question: "Should I", that becomes much more of a project-level decision. What are your goals? If you are looking to make some rather complex UI logic testable, then you might at least look into it. Fortunately, though, there are other patterns (Model-View-Presenter, for instance) that have more community backing that also has you write a testable "presenter" class. I find ViewModels significantly easier to write unit tests against compared to Presenters, but I think that is a personal preference.

顺便说一句,MVVM 模式主要是演示者模型"模式的另一个名称.您可能会查看是否有人在针对 WinForms UI 的演示者模型"方面取得了成功.

Just as an aside, the MVVM pattern is mostly another name for the "Presenter Model" pattern. You might look to see if anyone is having success with the "Presenter Model" against WinForms UIs.

祝你好运!

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

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