Prism Sample 11-UsingDelegateCommands
本例的知识点,全在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的更多相关文章
- Android 主题动态切换框架:Prism
		Prism(棱镜) 是一个全新的 Android 动态主题切换框架,虽然是头一次发布,但它所具备的基础功能已经足够强大了!本文介绍了 Prism 的各种用法,希望对你会有所帮助,你也可以对它进行扩展, ... 
- 从PRISM开始学WPF(六)MVVM(二)Command?
		从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ... 
- 从PRISM开始学WPF(六)MVVM(二)Command-更新至Prism7.1
		命令绑定(Command) [7.1updated]这一节除了基础app部分,并没有什么变化 什么是Command? 先看下微软官方的说明: Commanding is an input mechan ... 
- [转]Blue Prism Architecture
		本文转自:https://mindmajix.com/blue-prism-architecture Introduction Automation technology is widely bloo ... 
- 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 ... 
- 扩展GridView控件——为内容项添加拖放及分组功能
		引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ... 
- Codeforces Round  #342 (Div 2)  解题报告
		除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ... 
- Java编程思想读书笔记
		声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ... 
- 数据结构 : Hash Table
		http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html 作者:Angel Lucifer 引子 这篇仍然不讲并行/并发. ... 
- AC Milan VS Juventus(模拟)
		AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Oth ... 
随机推荐
- 使用 ww.cad 完成dwg文件转shp(包含所有属性)
			单纯使用ArcEngine提供的接口读取dwg数据转shp存在众多属性无法读取的情况(最直观的 南方cass生产的dwg文件有SOUTH这一字段,为目标要素的类型) private void Conv ... 
- typescript 的动态引入组件
			环境: Arco Pro + Vue3 vite自身对动态字符串形式的组件引入是有限制的, 以下写法会报错 官方文档中也对此有做说明, 只能通过固定形式去引用 以下形式不会报错, 但这种固定格式的局限 ... 
- 织梦(DedeCMS)安全方案
			一.织梦(DedeCMS)介绍 织梦(DedeCMS)作为国内龙头CMS系统之一,因其系统结构科学合理,操作使用简单,拥有海量用户.和其他CMS一样,安全漏洞也是其无法避免的问题.虽然官方不断发布补丁 ... 
- 操作系统笔记<1>
			操作系统的概念 指控制和管理整个计算机系统的硬件和软件资源,并合理地组织调度计算机的工作和资源的分配 以提供给用户和其他软件方便的接口和环境 是计算机系统中最基本的系统软件 操作系统的功能 (系统资源 ... 
- RabbitMQ 消息中间件总结
			RabbitMQ 是实现高级消息队列协议(AMQP:Advanced Message Queue Protocol)的开源代理软件,也称为面向消息的中间件.支持多种操作系统.多种编程语言.Rabbit ... 
- Kafka 生产者写入数据
			一.生产者发送消息的步骤 
- 改善与提高linux系统的音质
			当初装Linux系统的时候,我就发现了,deepin和ubuntu的linux平台音质和Windows平台下的音质不一样.硬件参数没变,歌曲音频文件相同的情况下,linux播放歌曲的时候动态和音乐细节 ... 
- MySQL高性能索引策略和查询性能优化
			前缀索引和索引选择性 有时候需要索引很长的字符,这会让索引变得大且慢.一个策略是模拟哈希索引. 通常可以索引开始的部分字符,这样可以大大解约索引空间,提高索引效率.但这样会降低索引的选择性. 索引的选 ... 
- es6数组相关操作
			1. 获取两个数组中某个属性值相等的项 let a=[{name:1},{name:4},{name:3}] let b=[{name:5},{name:4},{name:2}] let index ... 
- 基于Admin.NET框架的前端的一些改进和代码生成处理(2)
			在上篇随笔<基于Admin.NET框架的前端的一些改进和代码生成处理(1)>中大致介绍了一些关于对Admin.NET框架的前端的改造工作,主要目的就是希望能够增加前端代码的简洁性和可读性, ... 
