MVVM全称:Model-View-ViewModel

优点:MVVM的引入使程序实现了松耦合设计,UI层与业务逻辑可以并行设计

1、Model:对现实世界的抽象

比如:需要做一个学校的管理系统,学校包括学生和老师,此时可以把老师和学生抽象成老师类及学生类。

老师类:具有教学及备课等属性

学生类:具有学习的属性

将这些实实在在的现实事物抽象成类。Model层永远不知道View层的存在。

2、ViewModel:对View层的抽象

还是以学校为例:在View界面上有需要显示老师的姓名、性别、教的什么课程,显示学生的姓名、性别、年龄、年级,此时可以将这些抽象成具体的ViewModel模型,包含老师及学生要显示的属性以及命令。

说明:属性需要具有通知UI更新的能力,即ViewModel要继承:NotificationObject类。

属性:实现接口INotifyPropertyChanged,自动和UI层交互。

集合:它的类型一般是ObservableCollection,因此,任何UI元素绑定到它,不管这个集合什么时候改变,都可以自动的与UI交互。

命令:要继承ICommand接口。

为了理解MVVM,下面一个简单的例子:

(a)、实现NotificationObject类:

public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

(b)、实现DelegateCommand类

class DelegateCommand : ICommand
{
public bool CanExecute(object parameter)
{
if (this.CanExecuteFunc == null)
{
return true;
} return this.CanExecuteFunc(parameter);
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
if (this.ExecuteAction == null)
{
return;
}
this.ExecuteAction(parameter);
} public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunc { get; set; }
}

(c)、实现ViewModel类

class MainWindowViewModel : NotificationObject
{
private double _name; public double Name
{
get { return _name; }
set
{
_name = value;
this.RaisePropertyChanged("Name");
}
} public DelegateCommand SaveCommand { get; set; } private void Save(object parameter)
{
this.Name += "保存";
            SaveFileDialog dlg = new SaveFileDialog();
dlg.ShowDialog();
} public MainWindowViewModel()
{
this.SaveCommand = new DelegateCommand();
this.SaveCommand.ExecuteAction = new Action<object>(this.Save);
}

3、View:UI层

<TextBox Text="{Binding Name}" />
<Button Content="Save" Command="{Binding SaveCommand}" />

在代码隐藏中加入

public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}

以上只是简陋的MVVM框架结构,只是帮助理解,而在实际的项目中,我们没必要再去编写NotificationObject及DelegateCommand这两个类,我们可以引用Prisim中的类即可,在项目中加入Microsoft.Practices.Prism.dll,NotificationObject 引用 using Microsoft.Practices.Prism.ViewModel;DelegateCommand引用using Microsoft.Practices.Prism.Commands;

最新文章

  1. java写RelativeLayout 的属性
  2. iOS & Mac JSON To Model
  3. MySQL创建数据库和表的Demo
  4. android 颜色对照
  5. Android开发--Intent的应用
  6. 简单的抓取淘宝关键字信息、图片的Python爬虫|Python3中级玩家:淘宝天猫商品搜索爬虫自动化工具(第一篇)
  7. 《30天自制操作系统》07_day_学习笔记
  8. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
  9. jetty服务器
  10. Eclipse Removing obsolete files from server 问题
  11. opencv 批量图像读写
  12. java设计模式--行为型模式--命令模式
  13. Linux进程间通信——信号集函数
  14. Visual C++学习笔记1:一定要注意ANSI和UNICODE差额
  15. 在win8.1 64位系统+cocos2d-x2.2.3下搭建android交叉编译环境
  16. android 5.0新特性学习--Drawable Tinting(为图片资源着色)
  17. kafka使用场景
  18. Django2.0文档
  19. POJ 3280 Cheapest Palindrome (区间DP) 经典
  20. JavaWeb中四大域对象的作用范围

热门文章

  1. Linux网络编程--洪水攻击详解
  2. 【Demo】CSS3元素旋转、缩放、移动或倾斜
  3. UVA-1605 Building for UN (构造)
  4. IEnumerable的用法
  5. @Column实体类中的使用(二十三)
  6. openfalcon源码分析之Judge
  7. eclipes常用快捷键
  8. scrollTop和scrollLeft属性
  9. python的单元测试代码编写流程
  10. Visual Studio2010 支持MVC4开发