MVVM 优于 MVC [英] Benefits of MVVM over MVC

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

问题描述

终于开始做一些 Silverlight 开发,我遇到了 MVVM.我对 MVC 很熟悉,我正在阅读的文章说,由于 XAML,MVC 行不通.没有太多的 XAML 经验显然是我没有明白这一点的原因.

Finally getting to do some Silverlight development and I came across MVVM. I am familiar with MVC and the article I was reading said because of XAML, MVC would not work out. Not having too much experience in XAML clearly is the reason I did not get this point.

谁能解释为什么 MVC 不适合以及为什么 MVVM 更适合 Silverlight 开发?

Can someone explain why MVC is not suited and why MVVM is better for Silverlight development?

谢谢京东

推荐答案

这是一个非常细微的区别,我可以通过比较 ASP.NET 中的 MVC 和 WPF 中的 MVVM 来最好地解释这一点.

Its a very slim distinction, which I can explain best by comparing MVC in ASP.NET and MVVM in WPF.

在 ASP.NET MVC 中,请求来自 Web 服务器并由控制器直接处理.控制器确定适当的视图并用模型填充它.然后控制器将这些实例释放给底层系统,底层系统将结果呈现给客户端.可以看到 Controller 是最先和最后一个动作的.

In ASP.NET MVC the request comes in from the web server and is handled directly by the Controller. The Controller determines the appropriate View and populates it with Models. The Controller then releases these instances to the underlying system which renders a result to the client. You can see that the Controller is first and last to act.

在 MVVM 中,UI(视图)面向用户并直接接受用户输入.在视图中,视图模型(即视图的数据上下文)中的命令由此活动触发.控制流向 ViewModel,它解释 V​​iew 发送的内容并准备其模型.在控制流回视图后,它会根据模型中的变化更新自身.如果需要一个新的 View,ViewModel 会与 NavigationService(或您的应用程序使用的任何导航方法)进行通信,这是 Window 或 Frame--UI 组件的权限.您可以看到 ViewModel 不是第一个也是最后一个动作;视图的作用比在 MVC 中要大得多.

In MVVM, the UI (the View), faces the user and takes user input directly. Within the View, Commands within the ViewModel (which is the DataContext of the View) are triggered by this activity. Control flows to the ViewModel which interprets what the View has sent it and prepares its Models. After control flows back to the View it updates itself according to changes in the Models. If a new View is required, the ViewModel communicates this with the NavigationService (or whatever method of navigation your application uses), which is the purview of the Window or Frame--UI components. You can see that the ViewModel isn't first and last to act; the View plays a much greater role than in MVC.

WPF/Silverlight 的架构是这样做的原因.控制器无法控制/替换命令、绑定和导航基础设施;它们与 UI 紧密集成.所以控制器必须坐在视图下面并扮演更被动的角色.

The architecture of WPF/Silverlight is the reason why things are done this way. The command, binding and navigation infrastructures can't be controlled/replaced by the Controller; they are tightly integrated with the UI. So the Controller must sit below the View and take a more passive role.

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

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