本例的知识点,全在ViewModel中,看代码:

 1  public class MainWindowViewModel : BindableBase
2 {
3 private bool _isEnabled;
4 public bool IsEnabled
5 {
6 get => _isEnabled;
7 set
8 {
9 SetProperty(ref _isEnabled, value);
10 ExecuteDelegateCommand.RaiseCanExecuteChanged();
11 }
12 }
13
14 private string _updateText;
15 public string UpdateText
16 {
17 get => _updateText;
18 set => SetProperty(ref _updateText, value);
19 }
20
21
22 public DelegateCommand ExecuteDelegateCommand { get; private set; }
23
24 public DelegateCommand<string> ExecuteGenericDelegateCommand { get; private set; }
25
26 public DelegateCommand DelegateCommandObservesProperty { get; private set; }
27
28 public DelegateCommand DelegateCommandObservesCanExecute { get; private set; }
29
30
31 public MainWindowViewModel()
32 {
33 ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);
34
35 DelegateCommandObservesProperty = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => IsEnabled);
36
37 DelegateCommandObservesCanExecute = new DelegateCommand(Execute).ObservesCanExecute(() => IsEnabled);
38
39 ExecuteGenericDelegateCommand = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);
40 }
41
42 private void Execute()
43 {
44 UpdateText = $"Updated: {DateTime.Now}";
45 }
46
47 private void ExecuteGeneric(string parameter)
48 {
49 UpdateText = parameter;
50 }
51
52 private bool CanExecute()
53 {
54 return IsEnabled;
55 }
56 }

构造函数中建立了4个命令。

其中,33行这个是原始的,CanExecute要靠10行的代码来通知改变。后面的三个,是使用语法糖

ObservesCanExecute(() => IsEnabled)实现了自动通知。
这里可以试一下,注释掉
ExecuteDelegateCommand.RaiseCanExecuteChanged();然后运行的话,勾选Enabled,其他三个命令都改变状态,而第一个按钮就没有收到变更通知。

39行构造的命令为泛型命令,支持一个泛型的参数。参数在xaml中使用CommandParammeter提供。

Prism Sample 11-UsingDelegateCommands的更多相关文章

  1. Android 主题动态切换框架:Prism

    Prism(棱镜) 是一个全新的 Android 动态主题切换框架,虽然是头一次发布,但它所具备的基础功能已经足够强大了!本文介绍了 Prism 的各种用法,希望对你会有所帮助,你也可以对它进行扩展, ...

  2. 从PRISM开始学WPF(六)MVVM(二)Command?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  3. 从PRISM开始学WPF(六)MVVM(二)Command-更新至Prism7.1

    命令绑定(Command) [7.1updated]这一节除了基础app部分,并没有什么变化 什么是Command? 先看下微软官方的说明: Commanding is an input mechan ...

  4. [转]Blue Prism Architecture

    本文转自:https://mindmajix.com/blue-prism-architecture Introduction Automation technology is widely bloo ...

  5. Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)

    传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...

  6. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  7. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  8. Java编程思想读书笔记

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. 数据结构 : Hash Table

    http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html 作者:Angel Lucifer 引子 这篇仍然不讲并行/并发. ...

  10. AC Milan VS Juventus(模拟)

    AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

随机推荐

  1. layui 手册

    https://layui.yii666.com/doc/modules/layer.html

  2. 推荐2020年最好用的JavaScript代码压缩工具

    今天就为大家分享一篇关于推荐2020年最好用的JavaScript代码压缩工具,觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随来看看吧 JavaScript 代码压缩是指去除 ...

  3. python内置函数range()—对象创建函数

    range()函数 介绍 range()函数实际上表示一个不可变的数字序列类型,通常用于在for循环中指定特定的次数. range()的格式: range(stop) range(start, sto ...

  4. RPA的应用及工作原理

    通过本章学习,您将了解到: 什么是RPA RPA能做些什么 RPA的应用有什么特点 RPA是怎样进行工作的 RPA怎么实现人机协作 RPA的未来趋势怎么样 什么是RPA RPA是利用软件来执行业务流程 ...

  5. 换个脑袋,做个小练习------四则运算系统的随机出题的jsp实现

    四则运算出题系统网页界面的实现(别期待,只有俩操作数) index.jsp <%@ page contentType="text/html;charset=UTF-8" la ...

  6. ovs-dpdk:revalidator源码解析

    revalidator是做什么的?需要知道哪些东西? 有关于revalidator需要弄明白的是以下三个问题: 通过ovs-vsctl list open_vs可以看到other_config里面有两 ...

  7. 布局管理器wx.BoxSizer

    b = wx.BoxSizer( wx.VERTICAL ) b.Add(self.notebook1, 1, wx.EXPAND) self.parent.SetSizer(b) 解析以上代码原理, ...

  8. springboot 接入 ChatGPT

    项目地址 https://gitee.com/Kindear/lucy-chat 介绍 lucy-chat是接入OpenAI-ChatGPT大模型人工智能的Java解决方案,大模型人工智能的发展是不可 ...

  9. 阿里云OSS前端直传+net core后端签名

    OSS前端直传+后端签名 一.服务端签名后前端直传 首先安装阿里云SDK Aliyun.OSS.SDK.NetCore public static string accessKeyId = " ...

  10. 总结workerman启动与停止相关命令

    手册:https://www.workerman.net/doc/workerman/install/start-and-stop.html 本篇文章给大家介绍一下workerman启动与停止相关命令 ...