一句话解释WPF中的MVVM? [英] One sentence explanation to MVVM in WPF?

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

问题描述

我听说它是​​构建 WPF UI 的次佳选择,但所有现有的示例有几十行代码 - 我可以得到一个 Hello World对于 MVVM,它毫不含糊地解释了它的全部内容?我对 C#/.net 也很陌生,所以也许可以指点我也可以提供帮助的资源?

I heard its the next best thing in building WPF UIs, but all existing examples have dozens of lines of code - can I get a Hello World for MVVM that explains in no uncertain terms what its all about? I'm fairly new to C#/.net as well, so maybe point me to some resources that could help too?

非常感谢!

推荐答案

一句话解释:

MVVM 是对广受欢迎的模型-视图-展示器 (MVP) 模式的重新构想,该模式旨在与 WPF 提供的数据绑定工具配合使用,以将应用程序逻辑与 UI 设计分开.

MVVM is a reimagining of the well loved Model-View-Presenter (MVP) pattern that is designed to work especially well with databinding facilities supplied with WPF to separate application logic from UI design.

更长、更有用的解释:

MVVM 的基本概念是将 WPF 应用程序分解为单独的组件,每个组件在屏幕上获取信息的过程中承担一个责任.

The basic concept of MVVM is the break apart a WPF application into separate components each of which has one responsibility in the process of getting information on screen.

首先你有模型.这是一个功能非常有限的类,通常从一些外部源(例如数据库或 Web 服务)填充.例如:

Firstly you have the model. This is a class with very limited functionality that is generally populated from some outside source such as a database or webservice. For example:

public class MessageModel
{
    public string Message { get; set; }
}

在此之上,您将 ViewModel 分层,这是应用程序逻辑所在的位置,它将模型更改通知视图并确保数据一致性.通过实现 INotifyPropertyChanged 接口,WPF 免费提供了 ViewModel 和视图之间的两种数据绑定:

On top of that you layer the ViewModel, this is where the logic of the application sits, it notifies the view of changes to the model and ensures data consistency. By implementing the INotifyPropertyChanged interface two way databinding between the ViewModel and the view is given for free by WPF:

public class MessageViewModel : INotifyPropertyChanged
{
     private MessageModel _model;

     public string Message
     {
          get { return _model.Message; }
          set
          {
              if (_model.Message != value)
              {
                  _model.Message = value;
                  OnPropertyChanged("Message");
              }
          }
     }
}

终于有了视图.这是一个 xaml 文件,描述了用于在 ViewModel 中显示和编辑数据的控件的布局:

Finally you have the View. This is a xaml file that describes the layout of the controls used to display and edit the data in the ViewModel:

<Canvas>
     <TextBox Text={"Binding Message"} />
</Canvas>

您进行所有这些努力的原因是模型非常轻量级,并且很容易跨域边界传递.从网络服务发送或接收它或将其映射到数据库表很简单.另一方面,ViewModel 很复杂,但依赖项很少——它不关心模型从哪里获取数据,只关心它在那里并且根本没有视图的概念,这使得它非常可测试(应用程序的逻辑不依赖于 UI 来测试).最后,xaml 被很好地划分开来,可以交给对应用程序逻辑一无所知的设计者,只需要 ViewModel 将以某些名称呈现某些数据.这种封装使得在大型项目中定义角色变得非常容易,或者将有限的 UI 放在一起以测试逻辑,同时完善真实的 UI.

The reason that you go to all this effort is that the Model is very lightweight and easily passed across domain boundaries. It is simple to send or receive it from a webservice or map it to a database table. The ViewModel, on the other hand is complex, but has few dependencies - it doesn't care where the model gets it's data from, only that it is there and it has no concept of a view at all which makes it very testable (the logic of your application doesn't rely on a UI to test). Finally the xaml is well compartmentalised and can be handed off to a designer who needs to know nothing about the logic of the application, only that the ViewModel will present certain data under certain names. This encapsulation makes it very easy to define roles in large projects, or put together a limited UI to test logic against while the real one is being polished.

这篇关于一句话解释WPF中的MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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