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. 大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)

    大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)大前端时代已经到来!如今,前端开发工程师的职责,不是只有切图.制作网页这么简单哦! G:\传智播客2015-WEB前端视频教 ...

  2. [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组

    13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...

  3. Bootstrap2和3的区别

    如果你需要兼容IE8甚至是IE7和IE6,那么只能选择Bootstrap2,虽然它自身在IE6的效果也并不完美.     但是倘若你跟随时代的脚步,并且面向的客户也很高端大气上档次地选择只需要兼容高级 ...

  4. QQ提醒的功能

    原文出处:http://www.cnblogs.com/xiexingen/archive/2013/04/09/3009921.html http://qzs.qq.com/snsapp/app/b ...

  5. [wikioi 1418]铃仙•优昙华院稻叶(东方幻想乡系列模拟赛)(树上递推)

    题目:http://www.wikioi.com/problem/1418/ 分析: 一看就肯定是树上的递推 设f[i][j][k]表示第i秒在k点(从j点走过来的)的概率 则f[i][j][k]=f ...

  6. WCF 入门(19)

    前言 天气转凉,提前过冬了.感冒依旧没好,因为双休日伙食太好了,各种鱼各种肉. 第19集 创建然后抛出强类型的SOAP faults  Creating and throwing strongly t ...

  7. nginx + Lua 实现自定义WAF

    文章摘自:https://github.com/unixhot/waf wget git@github.com:unixhot/waf.git

  8. ansible-2添加公钥

    该文章摘自:http://www.fwqtg.net/%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7ansible%E5 ...

  9. Java-日期转换

    如下: package 时间日期类; import java.text.SimpleDateFormat; import java.util.Date; public class 日期格式转换 { / ...

  10. Hibernate-一级缓存session

    hibernate提供的一级缓存 hibernate是一个线程对应一个session,一个线程可以看成一个用户.也就是说session级缓存(一级缓存)只能给一个线程用,别的线程用不了,一级缓存就是和 ...