WPF个人文档(六)—— 命令
命令最大的作用就是前后端解耦,它并不是事件的上位替代品!!!
一.概念
- 我们先来看看命令这两个字,在汉语上的词意
- 命令:一种权威性的指示,通常指上级对下级的口头或书面指示,要求其执行某项任务或采取某种行动
命令往往具有明确的目标和要求,并带有一定的强制性
- 然后我们再来看看WPF中命令的概念
- 命令:代指一种明确的指令或者要求,用于向某个目标传递指定的操作或者行为
- 因此,只从效果上来看,命令和事件的效果视乎一样,但是,命令 ≠ 事件
- WPF中的命令是一种行为抽象机制,它将UI触发、执行逻辑和作用目标解耦,并通过CanExecute实现状态驱动的交互控制
- 命令是对"行为"的对象化封装
- 它将“触发者”、“执行逻辑”和“作用对象”解耦(前后端解耦),并提供“是否可执行”的状态控制机制(行为约束)
- 命令(Command)= 把 "一个动作" 抽象成 一个对象
- 在现实生活中,假设你在对一个AI发起了一个命令,那么我们的流程是
- 编辑执行命令内容 -> 选择命令目标 -> 告诉AI执行什么操作 -> 行为约束
- # 其中涉及到
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>命令发出者
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>命令接收者
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>命令内容
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>命令执行者
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>开始执行命令
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>完成执行内容
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>汇报执行结果
复制代码- 而我们使用的命令,和这个流程其实非常相似(参考小结中的流程图)
二.命令的四个概念
- 命令源(Command Source)
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings><Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>↓ # 触发
- 命令(Command)
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings><Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>↓ # 查找
- 命令绑定(Command Binding)
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings><Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>↓ # 执行
- 命令目标(Command Target)
复制代码 1.0 命令(Command) —— 继承ICommand接口
- 本质上其实就是实现了一个ICommand对象
- 主要负责2件事情
- Execute(执行逻辑)
- CanExecute(能不能执行)
1.1 预定义命令库✳
- 预定义命令库:WPF内置的一组标准命令(已经实现好的 ICommand)
- 不用写 RelayCommand了,开袋即食,别造轮子了
- 预定义命令 = 官方帮你定义好的“行为规范”,而不是具体实现
- 常用的命令库(预定义)有五个静态类
- ApplicationCommands # 应用通用
- NavigationCommands # 导航(页面跳转)
- EditingCommands # 文本编辑
- ComponentCommands # 组件
- MediaCommands # 媒体控制
- # 使用时,请随用随查,而且除了第一个,基本上不怎么使用,了解即可
- # 使用频率大概是:
- ApplicationCommands >>> EditingCommands > NavigationCommands > MediaCommands > ComponentCommands
复制代码
- 示例代码:
- 新建一个操作,这个操作绑定到逻辑CommandBinding_Executed上
- <Window.CommandBindings>
- <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
- </Window.CommandBindings>
复制代码
- 下面大致列举了一部分常用的预定义命令:
- 随用随查,别背,没用,顶多记一下ApplicationCommands通用类的几个,毕竟大多数都用不上
(1)ApplicationCommands应用通用类
- 命令作用常见场景Copy复制选中内容TextBox / 数据编辑Cut剪切选中内容文本编辑Paste粘贴剪贴板内容输入框Save保存数据文件/表单SaveAs另存为文件系统Open打开文件文件操作New新建内容编辑器Undo撤销操作编辑器Redo重做操作编辑器Delete删除选中项列表/文本Print打印内容报表/文档
(2)NavigationCommands导航(页面跳转)类
- 命令作用常见场景Back返回上一页页面导航Forward前进页面导航Refresh刷新当前页面Web/数据页BrowseHome返回首页应用首页BrowseStop停止加载浏览器
(3)EditingCommands文本编辑类
- 命令作用常见场景Delete删除内容文本编辑Backspace删除前一个字符输入框ToggleBold加粗文本富文本ToggleItalic斜体文本富文本IncreaseFontSize增大字体编辑器DecreaseFontSize减小字体编辑器
(4)ComponentCommands组件类
- 命令作用常见场景MoveUp向上移动项列表MoveDown向下移动项列表MoveLeft向左移动UI操作MoveRight向右移动UI操作ExtendSelection扩展选区多选控件
(5)MediaCommands媒体控制类
- 命令作用常见场景Play播放媒体视频/音频Pause暂停播放媒体控制Stop停止播放媒体控制Record开始录制录音NextTrack下一首音乐播放器PreviousTrack上一首音乐播放器VolumeUp音量增加媒体控制VolumeDown音量减少媒体控制
2.命令源<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed"/>
</Window.CommandBindings>—— 继承ICommandSource
<ul>调用命令的对象,通常是UI控件
- 直白点说,就是,谁触发了命令,那个谁就是命令源
- # 这里的命令源就是Button
- <Button Command="{Binding SaveCommand}" />
复制代码
命令源相关属性一般有3个,上面那个是最常用的,下面两个也了解一下
<ul>第三个命令目标详细请见下一个小点
[table][tr]属性作用备注[/tr][tr][td]
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|
|
|
|
|
相关推荐
|
|
|