zoukankan      html  css  js  c++  java
  • 理解MVVM模式

    1、WPF的核心是数据绑定。

    2、考虑这样一个场景:界面上有一个TextBox显示Person的年龄,一个Button,点击一次Button,年龄加1。

    3、做一个View,上面有TextBox和Button,TextBox的Text绑定ViewModel中Person的年龄,Button的Command绑定ViewModel中的命令。

    4、设置View的DataContext为ViewModel

    5、ViewModel关联Person和Command对象,这里的Person就是Model。ViewModel对View暴露两个接口:Person的Age和Command。

    6、Command封装Person的方法,用以改变Person的年龄。

    7、Person对ViewModel暴露两个接口:Age属性和AddAge()方法。

    8、ViewModel中的Command要实现接口ICommand。

    9、Person要实现INotifyPropertyChanged接口或者继承DependencyObject,才能具备自动更新UI的能力。

    10、总结:Model对ViewModel暴露属性和方法,方法改变自身的属性值;ViewModel对View暴露Model的属性和Command,Command封装Model的方法;View的DataContext设置为ViewModel,View中的控件绑定ViewModel暴露的Model属性和Command。

    11、ViewModel与View是一对多的关系。

    12、ViewModel的作用是对View和Model解耦,使View间接关联Model,Model不知道ViewModel的存在,ViewModel不知道View的存在。也就是说,View单向关联ViewModel,ViewModel单向关联Model。

  • 相关阅读:
    .Net Core Swagger配置
    MySQL如何使用索引
    一个HTTP Basic Authentication引发的异常
    跑步花钱吗?
    跑步花钱吗?
    OpenShift中的持续交付
    在AWS中部署OpenShift平台
    壮美大山包-2017中国大山包国际超百公里ITRA积分赛赛记
    膝盖中了一箭之康复篇-两周年纪念
    HashiCorp Vault介绍
  • 原文地址:https://www.cnblogs.com/nzbbody/p/3388433.html
Copyright © 2011-2022 走看看