好久没来写博客了,这期间经历了春节,也因为忙于一个项目,所以博客被疏忽了。最近一段时间一直在用silverlight做项目,从来一开始的不熟悉渐渐的开始上手。今天记录一下自己学习prism的一些samplecode。
silvierlight目前的主流架构是Silverlight+MVVM+WCF RIA,说来惭愧本人做项目的时候对设计模式不是很了解。MVVM设计模式是指模型(Model)-视图(View)-视图模型(ViewModel),MVVM设计模式能够将程序的UI设计和逻辑设计分开,这样能够节省开发人员的大量时间,也可以使代码更容易维护和升级等。View是指UI,是用来展示的,Model可以定义一些数据访问的实体类,ViewModel是连接model层和view层的桥梁,它是中间层,主要用来一些业务逻辑的设计,这里包括与数据库的交互。
Prism是微软提供的一个用于Silverlight和WPF开发的框架。
下面重点讲讲Prim+MVVM的实现。
1.需要新建一个Silverlight应用程序,分为Silverlight服务端和客户端两部分,需要在Silverlight客户端添加View、Model、ViewModel几个文件夹,分别对应MVVM设计模式。
2.在Model中添加类Questionnaire
1 /// <summary> 2 /// 定义Model,如果需要监听属性的变化,需要继承INotifyPropertyChanged类 3 /// </summary> 4 public class Questionnaire:INotifyPropertyChanged 5 { 6 private string favoriteColor; 7 public Questionnaire() 8 { 9 } 10 public event PropertyChangedEventHandler PropertyChanged; 11 12 public string Name { get; set; } 13 14 public int Age { get; set; } 15 16 public string Quest { get; set; } 17 18 public string FavoriteColor 19 { 20 get 21 { 22 return this.favoriteColor; 23 } 24 25 set 26 { 27 //监听颜色属性的变化 28 if (value != this.favoriteColor) 29 { 30 this.favoriteColor = value; 31 if (this.PropertyChanged != null) 32 { 33 this.PropertyChanged(this, new PropertyChangedEventArgs("FavoriteColor")); 34 } 35 } 36 } 37 } 38 private string getText; 39 public string GetText 40 { 41 get { return getText; } 42 set 43 { 44 //监听字符的变化 45 if (value != this.getText) 46 { 47 this.getText = value; 48 if (this.PropertyChanged != null) 49 { 50 this.PropertyChanged(this, new PropertyChangedEventArgs("GetText")); 51 } 52 } 53 } 54 } 55 }