MVVM设计模式之精髓简化
MVVM 精要
1 Model :对现实对象的抽象。基本概括为都有哪些属性 。
2 ViewModel:是美一个界面的xaml的 数据通讯类。 View是从ViewModel类那找对应Binding的数据。
ViewModel的基本命名规则就是 View界面的名称加ViewModel后缀(MainWindowViewModel),ViewModel
中要暴露出所有View页面所需求的属性Binding以及操作CommandBinding。ViewModel中要实现所有页面操
作CommandBinding的动作方法。为了使Binding生效,所有ViewModel类都应该实现
INotifyPropertyChanged接口,通常的做法是自定义一个ViewModel基类来继承该接口,以后所有的
ViewModel类都继承这个自定义基类,从而达到通知Binding属性改变的目的。
/// <summary> /// 实现INotifyPropertyChanged 接口的 具有属性改变通知的 ViewModel 基类。 /// </summary> public class NotificationObject:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// 对PropertyChangedEventHandler 的一个封装。 /// </summary> /// <param name="propertyName"></param> public void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged!=null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }
大部分程序员都喜欢优雅一点的代码风,代码中尽可能的少出现硬编码("propertyName")这类。
所以相对优雅的办法应该是这样实现:
/// <summary> /// NotificationObject 优雅扩展 ^_^ /// </summary> public static class NotificationObjectEx { public static void RaisePropertyChanged<T, TProperty>(this T notificationObject, Expression<Func<T, TProperty>> expression) where T : NotificationObject { var memberExpression = expression.Body as MemberExpression; if (memberExpression != null) { string propertyName = memberExpression.Member.Name; notificationObject.RaisePropertyChanged(propertyName); } else { throw new NotImplementedException(); } } }
如此在ViewModel中暴露属性的时候就可以写成这样:
private double input1; public double Input1 { get { return input1; } set { if (input1 == value) return; input1 = value; this.RaisePropertyChanged(p => p.Input1); } }
其中 使用this.RaisePropertyChanged(p => p.Input1); 来替换硬编码方式的 this.RaisePropertyChanged("Input1"); 因为敲这种硬编码灰常容易敲错...
当然还有一种解决错误办法的就是使用自定义 snippet ,用代码段实现,额跑题了。
3 View 也就是页面。View是从ViewModel暴露出的属性来取值。使用Binding,从而达到解耦的目的。
主要就是先把Model抽象出来。然后根据View界面操作需要来丰富ViewModel。MVVM的中心思想主要就这些,可为什么还是觉得自己应用到实践中总是那么费劲呢?
MvvmLight 、SimpleMvvm 这些还不想去琢磨,我想先自己完全手敲出来...
接下来该怎么学习呐?跪求高人点拨。