前言
最近在学习Stylet中Command="{s:Action 方法名}"的设计与实现,但要弄明白这个之前,必须对原生实现命令比较熟悉,一想我也很久没有自己实现原生的命令了,之前都是用Community.Mvvm库来实现,所以今天先来回顾一下,在WPF中如何实现原生的命令。
借助AI使用原生的WPF写法实现了一个跟Stylet例子Hello一样的效果:
WPF中如何使用命令
WPF命令是实现用户界面交互的核心机制,通过实现ICommand接口来封装可执行的操作。命令支持松耦合的UI设计,可以绑定到按钮、菜单等控件,实现统一的执行逻辑。WPF提供了丰富的内置命令如ApplicationCommands、NavigationCommands等,同时也支持自定义命令,便于实现撤销/重做、数据绑定等复杂功能。
现在先来看看这个例子中是如何使用命令的吧!!这个例子中自己实现了一个实现ICommand接口的RelayCommand类。
先来看看ICommand接口:- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>public interface ICommand
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>{
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>event EventHandler? CanExecuteChanged;
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>bool CanExecute(object? parameter);
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>void Execute(object? parameter);
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>}
复制代码 这个ICommand接口起到了什么作用呢?
- 统一命令规范:定义了命令的标准结构,包含执行方法Execute和状态判断方法CanExecute
- 实现命令绑定:允许UI控件(如Button、MenuItem)通过Command属性绑定到具体命令实现
- 控制可用性:CanExecute方法动态控制控件的启用/禁用状态,CanExecuteChanged事件通知UI更新状态
- 参数传递:通过parameter参数在UI和命令逻辑间传递数据
- 解耦UI与业务逻辑:将界面操作与具体实现分离,提高代码的可维护性和可测试性
在RelayCommand中:- private readonly Action<object?> _execute;
- private readonly Predicate<object?>? _canExecute;
复制代码 _execute (Action): 存储要执行的操作委托
_canExecute (Predicate?): 存储判断命令是否可执行的谓词委托,可为 null- public event EventHandler? CanExecuteChanged
- {
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> add => CommandManager.RequerySuggested += value;
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> remove => CommandManager.RequerySuggested -= value;
- }
复制代码 这里出现了一个CommandManager:
WPF 中的 CommandManager 是一个帮助类,位于System.Windows.Input命名空间。它并不负责“执行命令”,而是为整个命令系统(RoutedCommand / RoutedUICommand)提供基础支撑,核心职责可以概括为四类:
1、处理路由命令的 4 个附加事件
CommandManager 预定义了 4 个 static 的 RoutedEvent,都是附加事件,所有 UIElement 都可以通过它们监听或引发命令相关路由事件:
附加事件触发时机典型用途PreviewCanExecuteEvent准备询问某命令能否执行时触发(隧道)用于全局或父级拦截“能否执行”判断CanExecuteEvent同上,但为冒泡阶段本地逻辑判断命令当前是否可用PreviewExecutedEvent准备执行命令时触发(隧道)做执行前的统一拦截,例如日志、撤销栈ExecutedEvent同上,但为冒泡阶段实际执行业务逻辑(如 Save、Cut、Paste)这里出现了隧道与冒泡两个概念,该如何理解呢?
在 WPF 路由事件体系中,隧道(Tunneling)与冒泡(Bubbling)是指事件在可视化树上传递的两个方向,想象成“从上到下”还是“从下到上”即可。与命令系统结合时,理解这两个方向就等于知道“谁先被通知”、“谁可以打断谁”。
树结构:
Window → Grid → StackPanel → Button
这是典型的一棵可视化树。
隧道(Preview……)→ 从根向叶
PreviewCanExecute / PreviewExecuted 这类以 Preview 开头的事件,先由 Window 收到,再依次 Grid、StackPanel,最后才到达实际声明 CommandBinding / 声明 InputBindings 的那个 Button。
作用:你可以在高层(例如 Window 一级)拦截事件,做“统一处理”或“统一否决”,比如给所有按钮加日志、在全局禁止某些快捷键等。只要沿途某级标记 e.Handled = true,它就终止继续向下传递。
冒泡(……无 Preview)→ 从叶向根
隧道阶段结束后如果仍然 Handled == false,则进入冒泡阶段。方向反过来:Button 先收到,再依次 StackPanel、Grid、Window。
作用:一般在最具体元素(Button)里决定命令是否可用或执行,而父容器只做辅助行为,如更新状态栏、刷新菜单对勾等。同样可以用 e.Handled = true 阻止再向上传。
2、提供 4 组 Add xxx Handler / Remove xxx Handler 的快捷方法
这些只是对 UIElement.AddHandler、RemoveHandler 的二次封装,方便挂接或注销上述 4 种附加事件,省去记忆事件标识符或强制转换类型的麻烦。
3、维护全局命令“有效性”通知:RequerySuggested
事件定义:public static event EventHandler RequerySuggested;
作用:当系统条件变化(键盘焦点变化、文本被修改、网络状态变更等)时,所有命令需要重新询问“是否能执行”。WPF 内部的按钮、菜单项等在订阅此事件后,就会再次调用 ICommand.CanExecute 来决定 IsEnabled。
手动触发:CommandManager.InvalidateRequerySuggested(); 会立即引发该事件,从而强制刷新所有绑定命令的可执行状态。
4、提供“类级别” CommandBinding / InputBinding 注册
RegisterClassCommandBinding(Type type, CommandBinding commandBinding)
为指定类型(而不仅是某个实例)注册 CommandBinding,在所有实例共享同一组绑定逻辑,等同于在静态构造函数里写:- CommandManager.RegisterClassCommandBinding(
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>typeof(MyControl),
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>new CommandBinding(ApplicationCommands.Save, OnSaveExecuted, OnSaveCanExecute));
- RegisterClassInputBinding(Type type, InputBinding inputBinding)
复制代码 同样道理,为某个控件类统一注册快捷键:- CommandManager.RegisterClassInputBinding(
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>typeof(MyWindow),
- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>new KeyBinding(ApplicationCommands.Save, Key.S, ModifierKeys.Control));
复制代码 现在来看看整体流程:- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>
复制代码 在View中绑定这个命令。
刚开始这个命令不可执行:
是因为在ViewModel中是这样写的,首先在构造函数中这样写:- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>public ShellViewModel() <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>{ <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>SayHelloCommand = new RelayCommand( <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>execute: _ => ShowHelloMessage(), <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>canExecute: _ => CanSayHello <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>); <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>}
复制代码 其中控制是否能执行的,设置了一个属性来管理:- public bool CanSayHello => !string.IsNullOrEmpty(Name);
复制代码 命令执行的方法为:- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>private void ShowHelloMessage() <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>{ <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>MessageBox.Show($"Hello, {Name}", "Hello, Native WPF", MessageBoxButton.OK, MessageBoxImage.Information); <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/>}
复制代码 刚开始Name属性为空,所以CanSayHello为false,所以命令不能执行。
为什么输入东西就可以变成执行了呢?- public string Name { <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> get => _name; <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> set <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> { <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> if (SetProperty(ref _name, value)) <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> { <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> ((RelayCommand)SayHelloCommand).RaiseCanExecuteChanged(); <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> } <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> } }
复制代码 在RelayCommand中有一个RaiseCanExecuteChanged方法:- <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> public void RaiseCanExecuteChanged() <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> { <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> CommandManager.InvalidateRequerySuggested(); <Button Content="Say Hello"
- Command="{Binding SayHelloCommand}"
- Height="30"
- FontSize="14"/> }
复制代码 CommandManager.InvalidateRequerySuggested(); 是 WPF 中用于强制刷新命令的可执行状态的方法。所有绑定了ICommand的控件(如 Button、MenuItem 等)马上重新评估自己的 CanExecute 状态。
然后因为Name不为空,CanSayHello为True,这个命令就可以执行了。
点击按钮就会触发RelayCommand中的Execute方法:
在ViewModel的构造函数中。实例化了一个RelayCommand对象,并且将_ => ShowHelloMessage()这个委托赋值给了execute,所以触发命令之后就会执行ShowHelloMessage方法。
以上就是使用WPF原生的方法实现的一个使用命令的例子。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |