在WPF自学入门(十)WPF MVVM简单介绍中的示例彷佛运行起来没有什么问题,也能够进行更新。可是这并非咱们使用MVVM的正确方式。正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地下降了Xaml文件和CS文件的耦合度,分离界面和业务逻辑,因此咱们要尽量的在View后台不写代码。可是这个例子中,咱们将更新ViewModel的代码写在了View里,下一个例子中,咱们要经过命令(Command)的来将Button的事件分离出来。框架
由于本文中须要使用Command命令,咱们先来简单了解Command命令。在WPF中使用命令的步骤很简单函数
WPF中命令的核心是System.Windows.Input.ICommand接口,全部命令对象都实现了此接口。当建立本身的命令时,不能直接实现ICommand接口,而是要使用System.Windows.Input.RouteCommand类,该类已经实现了ICommand接口,全部WPF命令都是RouteCommand类的实例。在程序中处理的大部分命令不是RoutedCommand对象,而是RoutedUICommand类的实例,它继承自RouteCommand类。对象
WPF提供了一个很好的方式来解决事件绑定的问题--ICommand。不少控件都有Command属性,若是没有,咱们能够将命令绑定到触发器上。接下来咱们来先实现一个ICommand接口。ICommand须要用户定义两个方法bool CanExecute和void Execute。第一个方法可让咱们来判断是否能够执行这个命令,第二个方法就是咱们具体的命令。blog
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Text; 8 9 using System.Windows.Input; 10 11 12 13 /***********************做者:黄昏前黎明后********************************** 14 15 * 做者:黄昏前黎明后 16 17 * CLR版本:4.0.30319.42000 18 19 * 建立时间:2018-04-05 22:57:56 20 21 * 命名空间:Example3 22 23 * 惟一标识:b9043d4c-fdd7-4e0f-a324-00f0f09286d0 24 25 * 机器名称:HLPC 26 27 * 联系人邮箱:hl@cn-bi.com 28 29 * 30 31 * 描述说明: 32 33 * 34 35 * 修改历史: 36 37 * 38 39 * 40 41 *****************************************************************/ 42 43 namespace Example3 44 45 { 46 47 public class RelayCommand : ICommand 48 49 { 50 51 #region 字段 52 53 readonly Func<Boolean> _canExecute; 54 55 readonly Action _execute; 56 57 #endregion 58 59 60 61 #region 构造函数 62 63 public RelayCommand(Action execute) 64 65 : this(execute, null) 66 67 { 68 69 } 70 71 public RelayCommand(Action execute, Func<Boolean> canExecute) 72 73 { 74 75 if (execute == null) 76 77 throw new ArgumentNullException("execute"); 78 79 _execute = execute; 80 81 _canExecute = canExecute; 82 83 } 84 85 #endregion 86 87 88 89 #region ICommand的成员 90 91 public event EventHandler CanExecuteChanged 92 93 { 94 95 add 96 97 { 98 99 100 101 if (_canExecute != null) 102 103 CommandManager.RequerySuggested += value; 104 105 } 106 107 remove 108 109 { 110 111 112 113 if (_canExecute != null) 114 115 CommandManager.RequerySuggested -= value; 116 117 } 118 119 } 120 121 122 123 [DebuggerStepThrough] 124 125 public Boolean CanExecute(Object parameter) 126 127 { 128 129 return _canExecute == null ? true : _canExecute(); 130 131 } 132 133 134 135 public void Execute(Object parameter) 136 137 { 138 139 _execute(); 140 141 } 142 143 #endregion 144 145 } 146 147 } 148 149
咱们再在咱们的NameViewModel中声明一个ICommand字段:继承
1 #region 命令 2 3 void UpdateNameExecute() 4 5 { 6 7 this.UserName = "黄昏前黎明后"; 8 9 this.CompanyName = "中软易通科技"; 10 11 } 12 13 14 15 bool CanUpdateNameExecute() 16 17 { 18 19 return true; 20 21 } 22 23 24 25 public ICommand UpdateName { get { return new RelayCommand(UpdateNameExecute, CanUpdateNameExecute); } } 26 27 28 29 #endregion
<Button Content="更新" Command="{Binding UpdateName}" Margin="20"/>
看到上面的结果,彷佛目前为止咱们已经很好的解决了全部的问题。咱们看到运行的数据,事件都是绑定的,实现了界面的完美分离。实际在处理问题是好像须要考虑通用性,这时咱们可否把MVVM提取出来做为一个框架,来去更好的解决问题。下一次咱们一块儿来看看怎么进行提取成为通用框架。