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

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

问题描述

我听说它是​​构建WPF UI的最好的一件事,但所有现有的
示例都有几十行代码 - 我可以得到一个Hello World
的MVVM,这一点毫无疑问地解释了所有关于?
我对C#/。net也很新,所以也许指向一些可以帮助的
资源?



非常感谢!

解决方案

一句话解释:



MVVM是受欢迎的Model-View-Presenter(MVP)模式的重新设计,旨在与WPF提供的数据绑定工具特别合作,将应用程序逻辑与UI设计分离开来。



更长,更有用,解释:



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



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

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

除此之外,您可以对ViewModel进行分层,这是逻辑应用程序位于,它通知视图对模型的更改,并确保数据一致性。通过实现INotifyPropertyChanged接口,ViewModel和视图之间的双向数据绑定由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中显示和编辑数据的控件的布局:

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

您所做的所有努力的原因在于,该模型非常轻便,易于跨域传播边界。从Web服务发送或接收它或将其映射到数据库表很简单。另一方面,ViewModel是复杂的,但依赖性很少 - 它不在乎模型从哪里获取数据,只是它在那里,它没有一个视图的概念,这使得它非常可测试(您的应用程序的逻辑不依赖于UI进行测试)。最后,xaml是很好的区分,可以交给一个不需要了解应用程序逻辑的设计师,只有ViewModel会以某些名字显示某些数据。这种封装使得在大型项目中定义角色变得非常容易,或者将有限的UI放在一起,以测试逻辑,同时真正的抛光。


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?

Much appreciated!

解决方案

One sentence explanation:

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.

Longer, more useful, explanation:

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.

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; }
}

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");
              }
          }
     }
}

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>

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天全站免登陆