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. C#操作JSON

    http://www.cnblogs.com/LiZhiW/p/3624729.html C#操作JSON 1. .NET对JSON的支持介绍............................. ...

  2. GridView自定义删除操作

    今天,我们这里要说的就是在GridView里面如何新添加一行“删除”列,如何删除前弹出通知等. 首先,我们前端的代码如下: <asp:GridView ID="gridViewDxjk ...

  3. linux查看系统信息命令

    本文转载自江一<linux查看系统信息命令> # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /p ...

  4. AWS S3使用小结

    使用场景一:储存网站的图片,并能被任何人访问 1. 创建一个bucket,名字与需要绑定的域名一致. 例如,根域名是mysite.com,希望把所有图片放在pic.mysite.com下面,访问的时候 ...

  5. 第十章 系统级I/O

    第十章 系统级I/O 一.Unix I/O 1.一个unix文件就是一个m个字节的序列 2.unix外壳创建的每个进程开始时都有三个打开的文件:标准输入(0) .标准输出(1)和标准错误(-1). 二 ...

  6. 优秀技能经验及对java学习展望

    你有什么技能比身边人强 我觉得我并没有什么技能能够比身边90%的人强,我认为我是一个平庸的人,和身边的人应该是互有长短,互相帮助的. 关于优秀技能的成功经验 我虽然没有一个强过身边90%的人的技能,但 ...

  7. iOS—— static和const联合使用;使用static const 与 #define

    static和const联合使用:   static将一个全局变量变成局部变量   const将一个局部变量变成局部常量 // 定义了一个局部常量      static const CGFloat ...

  8. 窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆

    原文发表在我的博客主页,转载请注明出处 前言 不论是小算法或者大系统,堆一直是某种场景下程序员比较亲睐的数据结构,而在python中,由于数据结构的极其灵活性,list,tuple, dict在很多情 ...

  9. asp.net Core开启全新的时代,用视频来告诉你,学习就是这么SO easy。

    https://channel9.msdn.com/Blogs/NET-Core/What-is-NET-Core 系统大家多发布一些视频的资料,学习起来更方便!我看到很多人发布的博客里面有的时候对于 ...

  10. jquery基本方法

    jquery的delay sleep 与js的setTime的区别. delay 和sleep推荐不要用,太难用了. click on live delegate bind http://www.jb ...