在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新。但是这并不是我们使用MVVM的正确方式。正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地降低了Xaml文件和CS文件的耦合度,分离界面和业务逻辑,所以我们要尽可能的在View后台不写代码。但是这个例子中,我们将更新ViewModel的代码写在了View里,下一个例子中,我们要通过命令(Command)的来将Button的事件分离出来。
因为本文中需要使用Command命令,我们先来简单了解Command命令。在WPF中使用命令的步骤很简单
1.创建命令
2.绑定命令
3.设置命令源
4.设置命令目标
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。第一个方法可以让我们来判断是否可以执行这个命令,第二个方法就是我们具体的命令。
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
最后,我们再将事件绑定上这个Command:
<Button Content="更新" Command="{Binding UpdateName}" Margin="20"/>
运行一下,看结果。我们成功将事件分离了出来。
看到上面的结果,似乎目前为止我们已经很好的解决了所有的问题。我们看到运行的数据,事件都是绑定的,实现了界面的完美分离。实际在处理问题是好像需要考虑通用性,这时我们能否把MVVM提取出来作为一个框架,来去更好的解决问题。下一次我们一起来看看怎么进行提取成为通用框架。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有