1. MVVM

MVVM的设计模式最早于2005年由微软的WPF和Silverlight架构师John Gossman在他的博客中提到。

WPF中采用MVVM的架构可以获得以下好处:

1. 将UI和业务的设计完全分开,View只是ViewModel的消费者

2. 有助于我们区别并哪些是UI操作,哪些是业务操作,而不是将他们混淆

3.层与层之间耦合度降低,这一点非常符合面向对象(OOP)的思想。

2.MVVM 用图来表示,这个是从网上找的图,简单明了,省去了自己画。

 

3.下面来一步一步写代码吧!

3.1 在项目根目录创建Model文件夹,并新增一个实体类,PersonModel,实现INotifyPropertyChanged通知接口。

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVMDemo.Model
{
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private string name = "吃饭了";
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
} private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null) return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} public void Show(object o)
{
this.Name += ",吃饭了"; }
}
}

PersonModel

3.2 在项目根目录创建ViewModel文件夹,并新增一个PersonViewModel

 using MVVMDemo.Commands;
using MVVMDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVMDemo.ViewModel
{
public class PersonViewModel
{
public PersonCommand PersonCommand { get; set; }
public Person PersonModel { get; set; } public PersonViewModel()
{
this.PersonModel = new Person();
this.PersonCommand = new PersonCommand();
this.PersonCommand.ExecuteCommand = this.PersonModel.Show; } }
}

PersonViewModel

3.3 在项目根目录创建Commands文件夹,并新增一个PersonCommand,实现ICommand接口。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace MVVMDemo.Commands
{
public class PersonCommand : ICommand
{
public Func<object, bool> CanExecuteCommand = null;
public Action<object> ExecuteCommand = null; public bool CanExecute(object parameter)
{
if (CanExecuteCommand == null) return true;
return CanExecuteCommand(parameter);
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
if (ExecuteCommand == null) return;
ExecuteCommand(parameter);
} public void OnCanExecuteChanged()
{
if (CanExecuteChanged == null) return;
CanExecuteChanged(this, EventArgs.Empty);
}
}
}

PersonCommand

3.4 MainWindow.xaml调用

 1 <Window x:Class="MVVMDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="MainWindow" Height="434.432" Width="607.471">
5 <Grid Margin="0,0,2,5">
6
7 <TextBlock Name="lbShow" Text="{Binding PersonModel.Name}" Margin="10,274,-10,10" />
8 <Button Command="{Binding PersonCommand}" Height="20" Width="120" Margin="214,211,214,93" Content="MVVM"></Button>
9 <!-- <Button Height="20" Width="120" Content="{Binding ElementName=gb,Path=Value,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"></Button> -->
10 </Grid>
11 </Window>
 using MVVMDemo.ViewModel;
using System.Windows; namespace MVVMDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new PersonViewModel(); }
}
}

3.5测试结果

很简单的例子,希望能看懂,这是基础。

WPF MVVM模式的更多相关文章

  1. WPF MVVM模式的一些理解

    /*本文转自 http://www.cnblogs.com/sirkevin/archive/2012/11/28/2793471.html */ 使用WPF+Mvvm开发一年多,期间由于对Mvvm模 ...

  2. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  3. wpf mvvm模式下CommandParameter传递多参

    原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递多参数,就要稍微处理一下.我暂时还没找到更好的方案,下面介绍的这 ...

  4. WPF MVVM模式中,通过命令实现窗体拖动、跳转以及显隐控制

    原文:WPF MVVM模式中,通过命令实现窗体拖动.跳转以及显隐控制 在WPF中使用MVVM模式,可以让我们的程序实现界面与功能的分离,方便开发,易于维护.但是,很多初学者会在使用MVVM的过程中遇到 ...

  5. WPF MVVM模式下的无阻塞刷新探讨

    很多时候我们需要做一个工作,在一个方法体里面,读取大数据绑定到UI界面,由于长时间的读取,读取独占了线程域,导致界面一直处于假死状态.例如,当应用程序开始读取Web资源时,读取的时效是由网络链路的速度 ...

  6. WPF自学入门(十一)WPF MVVM模式Command命令

    在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式.正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地降低了 ...

  7. WPF MVVM模式下ComboBox级联效果 选择第一项

    MVVM模式下做的省市区的级联效果.通过改变ComboBox执行命令改变市,区. 解决主要问题就是默认选中第一项 1.首先要定义一个属性,继承自INotifyPropertyChanged接口.我这里 ...

  8. WPF自学入门(十二)WPF MVVM模式提取函数

    我们平时在写代码时为了不重复写代码,会进行复制代码或者写通用方法.今天我们就来把上传做的函数提取成为通用的方法调用.把上次写的函数提取为两个主要的文件:ObserableObject和RelayCom ...

  9. WPF MVVM模式下路由事件

    一,路由事件下三种路由策略: 1 冒泡:由事件源向上传递一直到根元素.2直接:只有事件源才有机会响应事件.3隧道:从元素树的根部调用事件处理程序并依次向下深入直到事件源.一般情况下,WPF提供的输入事 ...

随机推荐

  1. Go视频教程整理

    [Go Web基础]01博客项目设计 |Go视频教程|Go语言基础 http://www.tudou.com/programs/view/gXZb9tGNsGU/ [Go Web基础]02初窥 Web ...

  2. [Android] emualtor-5554 offline的解决方法

    现象:用adb devices命令总发现emualtor-5554 offline,在.android目录下面并没有发现这个设备,没法删除.原因:有程序占用5555端口,导致adb认为5554不能作为 ...

  3. 20135316王剑桥 linux第五周课实验笔记

    4.1.1程序员的可见的状态 ———— Y86的每条指令都会读取或修改处理器状态的某些部分,称为程序员可见状态.如图1所示. 1.程序寄存器(Program registers): %eax, %ec ...

  4. 20145215《Java程序设计》课程总结

    20145215<Java程序设计>课程总结 每周读书笔记链接汇总 20145215<Java程序设计>第一周学习总结 20145215<Java程序设计>第二周学 ...

  5. mod mono xsp

    Mod_Mono 是Apache的一个扩展模块,使得apache支持asp.net. 该模块传递asp.net的请求到一个额外的程序 mod-mono-server(该程序是在安装xsp的时候自动安装 ...

  6. php上传图片---初级版

    没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能. 图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性. 下面粘上代码: <? ...

  7. Dandelion - Distributed Computing on GPU Clusters

    linq on GPUs 非常期待中 看起来很cool,期望早点面世

  8. mongo里面根据对象字段的ID查询 db.Photo.find({'owner.$id':ObjectId('xxxx')}) , 并且使用forEach循环修改查询的数据

    var ones = db.Photo.find({'owner.$id':ObjectId("5344f0dab7c58e8e098b4567")}) db.Photo.find ...

  9. WebGame开发总结

    不知不觉我们的项目开发有2年了,这两年来走了很多弯路,也收获了很多,今天在这里做一个总结. 项目基本情况: 服务器端采用c++和c#混合开发,网络层采用c++开发,业务逻辑用c#开发.客户端采用sil ...

  10. iOS开发之UITextView,设置textView的行间距及placeholder

    一.设置textView的行间距 1.如果只是静态显示textView的内容为设置的行间距,执行如下代码: //    textview 改变字体的行间距     NSMutableParagraph ...