为什么 WPF/Silverlight 需要 MVVM,有什么不能使用 MVC 的充分理由吗?
也许我疯了,但我明白 MVC。这是有道理的。 MVVM 似乎是一种让 MVC 变得更难的方法,我错了吗(如果是这样,为什么?)?
Maybe I'm crazy, but I get MVC. It makes sense. MVVM seems like a way to make MVC harder, am I wrong (if so, why?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为,如果您了解 MVC,那么 MVVM 实际上是相同的 - 多了 1 个附加部分(ViewModel)。我认为 MVVM 更有用的原因与新的 DataBinding 流程有关。 ViewModel 提供了有关数据绑定内容以及如何呈现或转换该绑定的有用抽象和清晰度。这就是它在我脑海中所做的一切。
MVC 缺失的部分实际上是在 MVVM 模式中,在我看来它只是 MVVM 的隐藏部分。如果您查看大多数 MVVM 框架,大多数都提供某种消息传递或通知系统,这确实提供了 MVC 所缺少的控制器方面。
将 ViewModel 视为视图的代码隐藏,将视图视为视图的设计者,将模型视为模型,将通知/消息系统视为控制器,这样我们就有了一个真正等效的模式。
这对我来说也很难理解(我也来自 MVC 模式,但现在喜欢 MVVM 模式)。
I think that if you get MVC, then MVVM is really the same - with 1 additional part (the ViewModel). The reason that MVVM is more useful in my opinion has to do with the new DataBinding processes. The ViewModel provides useful abstraction and clarity about what is being databound, and how that binding is presented or converted. That's all it does in my mind.
The missing part from MVC is actually in the MVVM pattern, it's just the hidden part of MVVM in my opinion. If you look at most MVVM frameworks, most provide some sort of messaging or notification system, which really provides the missing Controller aspect of MVC.
Think of the ViewModel as the code-behind for your View, the View as the Designer of your View, the Model as the Model, and the Notification / Messaging system as the Controller, and we have a truely equivalent pattern.
This was a struggle for me to understand as well (I came from the MVC pattern as well, but now love the MVVM pattern).
也许这很有帮助。
Maybe this is helpful.
您不需要需要 MVVM - 它运行良好。
MVVM 只是一种替代方案。您可以自由使用 MVC。 MVVM 只是利用 WPF 和 Silverlight 中的一些特定功能(主要是令人惊叹的数据绑定支持,但也包括模板和命令),以使您作为开发人员的生活变得更简单。 MVVM 特别擅长将视图与代码解耦 - 甚至比 MVC 更出色,因为与 MVC 相比,使用 DataBinding 允许设计人员更有效地更改视图,而无需担心代码更改。
MVVM 的另一个优点是与 WPF 和 Silverlight 背后的基本设计原则密切相关。 MVVM 的基本思想在相当低的层面上与 WPF 和 Silverlight 的设计非常相似 - 例如,看看模板如何与自定义控件一起工作,并将其与 View/ViewModel 分离进行比较,有很多相关性可以绘制的。
You don't need MVVM - it just works well.
MVVM is just an alternative. You are free to use MVC. MVVM just takes advantage of some specific features in WPF and Silverlight (mainly the amazing DataBinding support, but also templating and commanding) in order to make your life, as a developer, simpler. MVVM is particularly good as decoupling the View from the code - even more so than MVC, as using the DataBinding allows a designer to change the View much more effectively without worrying about code changes than MVC.
MVVM also has the benefit of being very closely related to the fundamental design principles behind WPF and Silverlight. The basic idea of MVVM is very similar to the design of WPF and Silverlight at a fairly low level - For example, look at how templating works with custom controls, and compare it to the View/ViewModel separation, and there are a lot of correlations that can be drawn.