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

本文是小编为大家收集整理的关于对WPF中的MVVM的一句话解释?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我听到了它在构建WPF UIS中的下一个最好的事情,但所有现有 示例有数十行代码 - 我可以得到一个Hello World 对于MVVM,毫无疑问地解释了它的全部内容? 我也是C#/.网的新手,所以也许我要指出一些 也可以提供帮助的资源?

非常感谢!

推荐答案

一个句子说明:

MVVM是对良好喜爱的模型视图 - 总计(MVP)模式的重新构想,该模式旨在与WPF提供的数据指标设施特别好,以将应用程序逻辑与UI Design分开.

.

更长,更有用,解释:

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

首先您有模型.这是一个非常有限的功能的类,通常来自某些外部来源,例如数据库或WebService.例如:

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

最重要的是,您将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>

您付出所有这些工作的原因是该模型非常轻巧,并且很容易通过域边界.从WebService发送或接收它或将其映射到数据库表非常简单.另一方面,ViewModel很复杂,但几乎没有依赖性 - 它不在乎模型从何处获取数据,只有它在那里,并且根本没有视图的概念应用程序的逻辑不依赖UI进行测试).最后,XAML被很好地划分了,可以将其交给设计师,该设计师需要对应用程序的逻辑不了解,只有ViewModel会以某些名称显示某些数据.这种封装使在大型项目中定义角色变得非常容易,或者将有限的UI放在一起测试逻辑时,在抛光真正的项目时.

.

其他推荐答案

mVVM是星扇关系.球迷知道明星,但恒星不知道风扇.球迷非常热爱他的明星,以至于如果明星改变自己(我的意思是他的打扮风格),球迷就会相应地改变自己.

现在,用" ViewModel"和" View"替换" Star",然后再次阅读.

其他推荐答案

一句话?这里.

mVVM是一个UI分离模式,其中XAML(视图)与立面(视图模型)结合,允许程序的肠道(模型)避免使UI涉及泄漏的层.

.

本文地址:https://www.itbaoku.cn/post/627724.html

问题描述

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.

其他推荐答案

MVVM is a star-fan relationship. The fan knows the star but the star does not know the fan. The fan loves his star so much that if the star changes himself ( I mean his dressing style ), the fan changes himself accordingly.

Now replace "star" with "ViewModel" and "fan" with "View" and read it once again.

其他推荐答案

One sentence? Here goes.

MVVM is a UI segregation pattern where the Xaml (View) binds to a facade (View Model) allowing the guts of your program (Model) to avoid having UI concerns leak down a layer.