在上一篇写了Model的INotifyPropertyChanged接口实现,在Prism中有一个NotificationObject自动实现了这个接口,位于Microsoft.Practices.Prism.ViewModel命名空间下。也就是说,Prism推荐ViewModel继承这个NotificationObject类来自动实现INotifyPropertyChanged接口。看看NotificationObject都有啥:
1 public abstract class NotificationObject : INotifyPropertyChanged
2 {
3 protected NotificationObject();
4
5 protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);
6 protected void RaisePropertyChanged(params string[] propertyNames);
7 protected virtual void RaisePropertyChanged(string propertyName);
8 }
提供了几个很方面的接口,调用更方便了,例如:
1 public string ModelName
2 {
3 get { return _ModelName; }
4 set
5 {
6 _ModelName = value;
7
8 RaisePropertyChanged("ModelName");
9
10 }
11 }
第二个RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);是一个表达式,编译为一个Func委托,返回一个T类型。
例如可以这样用:
1 this.RaisePropertyChanged(() => this.MyDataSummary);
ViewModel的INotifyPropertyChanged接口和Model的INotifyPropertyChanged接口
ViewModel和Model它们二者都实现INotifyPropertyChanged接口并不矛盾。用途不一样。例如一个ViewModel可以包含多个其它的ViewModel,而它们有一个整体的HasChanged属性来标识是否有改变。这个时候这个整体的ViewModel的HasChanged属性就可以用整体的INotifyPropertyChanged,而局部的INotifyPropertyChanged实现了这个整体的INotifyPropertyChanged。看个例子:
1 using Microsoft.Practices.Prism.ViewModel;
2
3 public class MyViewModel3: NotificationObject
4 {
5 public MyModel MyModelData { get; set; }
6 public MyModel2 MyModelData2 { get; set; }
7
8 public bool HasChanges { get; set; }
9 public bool CanSave { get; set; }
10
11 public MyViewModel3(MyModel model, MyModel2 model2)
12 {
13 MyModelData = model;
14 MyModelData2 = model2;
15
16 model.PropertyChanged += this.OnPropertyChanged;
17 }
18
19 private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
20 {
21 if (args.PropertyName == "Name")
22 {
23 this.HasChanges = true;
24 this.RaisePropertyChanged(() => this.CanSave);
25 }
26 }
27 }
28
29 public class MyModel2 : INotifyPropertyChanged
30 {
31 public event PropertyChangedEventHandler PropertyChanged;
32
33 public int ModelID { get; set; }
34
35 private string _ModelName;
36 public string ModelName
37 {
38 get { return _ModelName; }
39 set
40 {
41 _ModelName = value;
42
43 if (PropertyChanged != null)
44 {
45 PropertyChanged(this, new PropertyChangedEventArgs("ModelName"));
46 }
47 }
48 }
49 }
50
51 public class MyModel : INotifyPropertyChanged
52 {
53 public event PropertyChangedEventHandler PropertyChanged;
54
55 public int ModelID { get; set; }
56
57 private string _ModelName;
58 public string ModelName
59 {
60 get { return _ModelName; }
61 set
62 {
63 _ModelName = value;
64
65 if (PropertyChanged != null)
66 {
67 PropertyChanged(this, new PropertyChangedEventArgs("ModelName"));
68 }
69 }
70 }
71 }
此外,Validation既可以放在Model里面也可以放在ViewModel里面,看你的规则是否简单,是否涉及业务逻辑,有的复杂的业务逻辑validation的需要调用后台service的建议放到ViewModel中去做。
分享到:
相关推荐
主要对mvvm框架的设计ICommand和INotifyPropertyChanged接口的使用
[Microsoft Press] Prism MVVM 模式应用开发指南 (WPF 和 Silverlight 实现)(英文版) [Microsoft Press] Developer's Guide to Microsoft Prism 4 Building Modular MVVM Applications with Windows Presentation ...
如何创建更好的Silverlight程序系列课程(3):Silverlight & MVVM
使用Prism7开发WPF程序,编码采用MVVM形式。当程序启动时,首先进入一个登陆界面,进行登陆认证,认证成功后转入程序布局主窗口。 设计思路 WPF程序框架搭建后,程序中存一个Shell.xaml,相当于表演者的唯一...
基于Prism框架的MVVM设计模式的WPF实例代码
Silverlight MVVM模式Silverlight MVVM模式Silverlight MVVM模式
"MVVM in Delphi: Architecting and Building Model View ViewModel Applications" 2016 | ISBN-10: 148422213X | 143 pages | PDF, EPUB | 23 MB Dive into the world of MVVM, learn how to build modern ...
WPF实现持久化笔记本,MVVM模式,依赖注入
实现mvvm模式 Silverlight
silverlight 中mvvm 的使用详解
Wpf Mvvm模式下窗口ShowDialog的实现
[Packt Publishing] 企业级架构 MVVM 模式指南 (WPF 和 Silverlight 实现) (英文版) [Packt Publishing] MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF (E-Book) ☆ 出版信息:☆ ...
一个经典的silverlight开发架构,使用了MVVM+PRISM对初学者来说再适合不过了
该资源使用MVVM编程模式,页面与逻辑分离的情况下,我们改动其中任何一个部分都是比较清楚的。
MVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMOMVVM PRISM DEMO
MVVM模式实现图片分页显示 (1)数据使用WCF服务加载 (2)定制ListBox数据模板 (3)采用MVVM模式实现分页 注意:在运行前需要修改WCF服务Service1.svc方法GetAllPictureData中的图片文件夹路径,默认是C:\Users\My...
MVVM设计模式简单案例,本案例分为多层实现,适合初学者使用。
这是一个很简单的利用Prism实现MVVM设计模式的简单小例子,希望对大家有用!
MVVM模式结合MVVMlight框架的应用 程序=数据结构+算法 面向对象=对象+对象之间关系 1.以数据为中心的开发方式。 1)Model:定义一个数据结构。 关键代码:实现接口INotifyPropertyChanged 2)ViewModel:定义算法...