用于Winforms的MVVM [英] MVVM for winforms

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

问题描述

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

Possible Duplicate:
UI Design Pattern for Windows Forms (like MVVM for WPF)

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对象很难测试,而ViewModels很容易测试.

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逻辑可测试,那么您至少可以研究一下.不过,幸运的是,还有其他模式(例如Model-View-Presenter)具有更多的社区支持,还可以编写可测试的"presenter"类.与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模式主要是"Presenter Model"模式的另一个名称.您可能希望查看是否有人在针对WinForms UI的"Presenter Model"上取得了成功.

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.

祝你好运!

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

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