ApplicationCommands: 这个静态类包含了一系列通用的应用程序级别的命令,例如剪切(Cut)、复制(Copy)、粘贴(Paste)、删除(Delete)、保存(Save)、打开(Open)等。这些命令与大多数应用程序的常规功能密切相关,可以直接绑定到UI控件上,以实现常见的交互行为。
MediaCommands: 提供了媒体播放相关的命令,如播放/暂停、停止、快进、倒退等,这些命令通常用于多媒体应用中的播放器组件控制。
NavigationCommands: 适用于导航场景的命令,如前进、后退、刷新等,在支持导航的应用或页面中非常有用。
ComponentCommands: 包含了一些针对特定组件操作的命令,如执行(Execute)、取消(Cancel)等。
其他:如EditingCommands、MenuCommands等多种内置命令集,分别对应不同类型的用户交互和功能需求。
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DeleteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
//执行
MessageBox.Show("删除操作命令已执行");
}
private void DeleteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// 判断命令是否可执行
e.CanExecute = true;
}
}
}
上述代码,按钮直接绑定了ApplicationCommands.Delete命令,当点击按钮时,将会执行相应的命令逻辑。同时,定义了CommandBinding,则可以自定义命令执行和是否可执行的方法。效果图如下:
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//设置了数据上下文(DataContext)并将MyViewModel实例绑定到了相应UI元素上
DataContext = new MyViewModel();
}
}
}
// MyViewModel 类实现
using CommunityToolkit.Mvvm.Input;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
public partial class MyViewModel : INotifyPropertyChanged
{
private ICommand? deleteCommand;
public event PropertyChangedEventHandler? PropertyChanged;
public ICommand DeleteCommand
{
get
{
if (deleteCommand == null)
{
deleteCommand = new RelayCommand(ExecuteDeleteCommand);
}
return deleteCommand;
}
}
private void ExecuteDeleteCommand()
{
//执行
MessageBox.Show("删除操作命令已执行");
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
定义一个继承自System.Windows.Input.RoutedCommand类,并提供命令名称和其他必要的元数据。
// 自定义类继承
using System.Windows.Input;
namespace WpfCommandApp
{
public class CustomRoutedCommand : RoutedCommand
{
public static readonly CustomRoutedCommand MyCustomCommand = new CustomRoutedCommand();
// 继承命令名称为MyCustomCommand
private CustomRoutedCommand()
: base("MyCustomCommand", typeof(CustomRoutedCommand), new InputGestureCollection() { new KeyGesture(Key.F5) }) //为命令绑定快捷键F5
{
}
}
}
注册命令路由:在应用程序启动时或窗口初始化阶段注册命令路由。
// 窗体类
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 为按钮绑定命令
MyButton.Command = CustomRoutedCommand.MyCustomCommand;
//注册命令路由
CommandBindings.Add(new CommandBinding(CustomRoutedCommand.MyCustomCommand, ExecuteMyCustomCommand, CanExecuteMyCustomCommand));
}
private void ExecuteMyCustomCommand(object sender, ExecutedRoutedEventArgs e)
{
//执行
MessageBox.Show("删除操作命令已执行");
}
private void CanExecuteMyCustomCommand(object sender, CanExecuteRoutedEventArgs e)
{
// 判断命令是否可执行
e.CanExecute = true;
}
}
}
实现一个模型继承ObservableObject
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
public class MyRelayCommandViewModel: ObservableObject
{
public ICommand CustomCommand => new RelayCommand(ExecuteCustomCommand, CanExecuteCustomCommand);
private void ExecuteCustomCommand()
{
// 实现命令执行逻辑
MessageBox.Show("自定义命令已执行");
}
private bool CanExecuteCustomCommand()
{
// 判断命令是否可执行
return true;
}
}
}
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 绑定上下文
DataContext = new MyRelayCommandViewModel();
}
}
using System.Windows;
using System.Windows.Input;
namespace WpfCommandApp
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//为按钮绑定命令
MyButton.Command = CustomRoutedCommand.MyCustomCommand;
//添加到命令集合
CommandBindings.Add(new CommandBinding(CustomRoutedCommand.MyCustomCommand, ExecuteMyCustomCommand, CanExecuteMyCustomCommand));
}
private void ExecuteMyCustomCommand(object sender, ExecutedRoutedEventArgs e)
{
//执行
string parameter = (string)e.Parameter;
MessageBox.Show("MyCommand 已执行,参数为: " + parameter);
}
private void CanExecuteMyCustomCommand(object sender, CanExecuteRoutedEventArgs e)
{
// 判断命令是否可执行
e.CanExecute = true;
}
}
}
公众号“点滴分享技术猿”