命令概述

命令是 Windows Presentation Foundation (WPF) 中的一种输入机制,与设备输入相比,它提供的输入处理更侧重于语义级别。 示例命令如许多应用程序均具有的“复制”、“剪切”和“粘贴”操作。

本概述定义 WPF 中有哪些命令、哪些类属于命令模型以及如何在应用程序中使用和创建命令。

本主题包含以下各节:

什么是命令

命令具有多个用途。 第一个用途是分隔语义和从执行命令的逻辑调用命令的对象。 这可使多个不同的源调用同一命令逻辑,并且可针对不同目标自定义命令逻辑。 例如,许多应用程序中均有的编辑操作“复制”、“剪切”和“粘贴”若通过使用命令来实现,那么可通过使用不同的用户操作来调用它们。 应用程序可允许用户通过单击按钮、选择菜单中的项或使用组合键(例如 Ctrl+X)来剪切所选对象或文本。 通过使用命令,可将每种类型的用户操作绑定到相同逻辑。

命令的另一用途是指示操作是否可用。 继续以剪切对象或文本为例,此操作只有在选择了内容时才会发生作用。 如果用户在未选择任何内容的情况下尝试剪切对象或文本,则不会发生任何操作。 为了向用户指示这一点,许多应用程序通过禁用按钮和菜单项来告知用户是否可以执行某操作。 命令可以通过实现 CanExecute 方法来指示操作是否可行。 按钮可以订阅 CanExecuteChanged 事件,如果 CanExecute 返回 false 则禁用,如果 CanExecute 返回 true 则启用。

虽然命令的语义在应用程序和类之间可保持一致,但操作的逻辑特定于操作所针对的特定对象。 组合键 Ctrl+X 调用文本类、图像类和 Web 浏览器中的“剪切”命令,但执行“剪切”操作的实际逻辑由执行剪切的应用程序定义。 RoutedCommand 使客户端实现逻辑。 文本对象可将所选文本剪切到剪贴板,而图像对象则剪切所选图像。 应用程序处理 Executed 事件时可访问命令的目标,并根据目标的类型采取相应操作。

WPF 中的简单命令示例

使用 WPF 中命令的最简单的方式是使用某一个命令库类中预定义的 RoutedCommand使用具有命令处理本机支持的控件,以及使用具有命令调用本机支持的控件。 Paste 命令是 ApplicationCommands 类中的预定义命令之一。 TextBox 控件含有用于处理 Paste 命令的内置逻辑。 MenuItem 类具有调用命令的本机支持。

以下示例显示了如何设置 MenuItem,以便在单击时它将调用 TextBox 上的 Paste 命令,假定 TextBox 具有键盘焦点。

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste" />
  </Menu>
  <TextBox />
</StackPanel>
// Creating the UI objects
StackPanel mainStackPanel = new StackPanel();
TextBox pasteTextBox = new TextBox();
Menu stackPanelMenu = new Menu();
MenuItem pasteMenuItem = new MenuItem();

// Adding objects to the panel and the menu
stackPanelMenu.Items.Add(pasteMenuItem);
mainStackPanel.Children.Add(stackPanelMenu);
mainStackPanel.Children.Add(pasteTextBox);

// Setting the command to the Paste command
pasteMenuItem.Command = ApplicationCommands.Paste;

// Setting the command target to the TextBox
pasteMenuItem.CommandTarget = pasteTextBox;
' Creating the UI objects
Dim mainStackPanel As New StackPanel()
Dim pasteTextBox As New TextBox()
Dim stackPanelMenu As New Menu()
Dim pasteMenuItem As New MenuItem()

' Adding objects to the panel and the menu
stackPanelMenu.Items.Add(pasteMenuItem)
mainStackPanel.Children.Add(stackPanelMenu)
mainStackPanel.Children.Add(pasteTextBox)

' Setting the command to the Paste command
pasteMenuItem.Command = ApplicationCommands.Paste

WPF 命令中的四个主要概念

WPF 中的路由命令模型可分解为四个主要概念:命令、命令源、命令目标和命令绑定:

  • 命令是要执行的操作。

  • 命令源是调用命令的对象。

  • 命令目标是在其上执行命令的对象。

  • 命令绑定是将命令逻辑映射到命令的对象。

在前面的示例中,Paste 命令是命令,MenuItem 是命令源,TextBox 是命令目标,命令绑定由 TextBox 控件提供。 值得注意的是,CommandBinding 并不总是由作为命令目标类的控件提供。 通常,CommandBinding 必须由应用程序开发者创建,否则 CommandBinding 可能会附加到命令目标的上级元素。

命令

WPF 中的命令是通过实现 ICommand 接口创建的。 ICommand 公开了两种方法 ExecuteCanExecute,以及一个事件 CanExecuteChangedExecute 执行与该命令关联的操作。 CanExecute 确定是否可以在当前命令目标上执行该命令。 如果集中管理命令操作的命令管理器检测到命令源中存在一个可能使已引发命令无效但尚未由命令绑定执行的更改,则会引发 CanExecuteChangedICommand 的 WPF 实现是 RoutedCommand 类,并且是本概述的重点。

WPF 中输入的主要源是鼠标、键盘、墨迹和路由命令。 面向设备程度更高的输入使用 RoutedEvent 通知应用程序页中的对象输入事件已发生。 RoutedCommand 也不例外。 RoutedCommandExecuteCanExecute 方法不包含该命令的应用程序逻辑,而是引发通过元素树通行和浮升的路由事件,直到遇到具有 CommandBinding 的对象。 CommandBinding 包含这些事件的处理程序,命令正是由这些处理程序执行。 有关 WPF 中事件路由的详细信息,请参阅路由事件概述

RoutedCommand 上的 Execute 方法引发命令目标上的 PreviewExecutedExecuted 事件。 RoutedCommand 上的 CanExecute 方法引发命令目标上的 CanExecutePreviewCanExecute 事件。 这些事件通过元素树通行和浮升,直到遇到一个具有针对该特定命令的 CommandBinding 的对象。

WPF 提供了分布在几个类中的一组常用路由命令:MediaCommandsApplicationCommandsNavigationCommandsComponentCommandsEditingCommands。 这些类仅由 RoutedCommand 对象构成,而不包含命令的实现逻辑。 实现逻辑由在其上执行命令的对象负责。

命令源

命令源是调用命令的对象。 命令源的示例有 MenuItemButtonKeyGesture

WPF 中的命令源通常实现 ICommandSource 接口。

ICommandSource 公开三个属性:CommandCommandTargetCommandParameter

实现 ICommandSource 的 WPF 类是 ButtonBaseMenuItemHyperlinkInputBinding。 单击 ButtonBaseMenuItemHyperlink 时,调用一个命令,当执行与其关联的 InputGesture 时,InputBinding 调用命令。

以下示例显示如何将 ContextMenu 中的 MenuItem 用作 Properties 命令的命令源。

<StackPanel>
  <StackPanel.ContextMenu>
    <ContextMenu>
      <MenuItem Command="ApplicationCommands.Properties" />
    </ContextMenu>
  </StackPanel.ContextMenu>
</StackPanel>
StackPanel cmdSourcePanel = new StackPanel();
ContextMenu cmdSourceContextMenu = new ContextMenu();
MenuItem cmdSourceMenuItem = new MenuItem();

// Add ContextMenu to the StackPanel.
cmdSourcePanel.ContextMenu = cmdSourceContextMenu;
cmdSourcePanel.ContextMenu.Items.Add(cmdSourceMenuItem);

// Associate Command with MenuItem.
cmdSourceMenuItem.Command = ApplicationCommands.Properties;
Dim cmdSourcePanel As New StackPanel()
Dim cmdSourceContextMenu As New ContextMenu()
Dim cmdSourceMenuItem As New MenuItem()

' Add ContextMenu to the StackPanel.
cmdSourcePanel.ContextMenu = cmdSourceContextMenu
cmdSourcePanel.ContextMenu.Items.Add(cmdSourceMenuItem)

' Associate Command with MenuItem.
cmdSourceMenuItem.Command = ApplicationCommands.Properties

通常,命令源将侦听 CanExecuteChanged 事件。 此事件通知命令源在当前命令目标上执行命令的能力可能已发生更改。 命令源可以使用 CanExecute 方法查询 RoutedCommand 的当前状态。 如果命令无法执行,命令源可禁用自身。 此情况的一个示例是 MenuItem,在命令无法执行时,它自身将灰显。

InputGesture 可以用作命令源。 WPF 中的两种输入笔势是 KeyGestureMouseGesture。 可以将 KeyGesture 视为键盘快捷方式,例如 Ctrl+C。 KeyGesture 由一个 Key 和一组 ModifierKeys 组成。 MouseGestureMouseAction 和一组可选的 ModifierKeys 组成。

为了将 InputGesture 用作命令源,它必须与一个命令相关联。 可通过几种方式来实现此目的。 其中一种方法是使用 InputBinding

以下示例演示如何在 KeyGestureRoutedCommand 之间创建 KeyBinding

<Window.InputBindings>
  <KeyBinding Key="B"
              Modifiers="Control" 
              Command="ApplicationCommands.Open" />
</Window.InputBindings>
KeyGesture OpenKeyGesture = new KeyGesture(
    Key.B,
    ModifierKeys.Control);

KeyBinding OpenCmdKeybinding = new KeyBinding(
    ApplicationCommands.Open,
    OpenKeyGesture);

this.InputBindings.Add(OpenCmdKeybinding);
Dim OpenKeyGesture As New KeyGesture(Key.B, ModifierKeys.Control)

Dim OpenCmdKeybinding As New KeyBinding(ApplicationCommands.Open, OpenKeyGesture)

Me.InputBindings.Add(OpenCmdKeybinding)

InputGesture 关联到 RoutedCommand 的另一种方法是将 InputGesture 添加到 RoutedCommand 上的 InputGestureCollection

以下示例演示如何将 KeyGesture 添加到 RoutedCommandInputGestureCollection 中。

KeyGesture OpenCmdKeyGesture = new KeyGesture(
    Key.B,
    ModifierKeys.Control);

ApplicationCommands.Open.InputGestures.Add(OpenCmdKeyGesture);
Dim OpenCmdKeyGesture As New KeyGesture(Key.B, ModifierKeys.Control)

ApplicationCommands.Open.InputGestures.Add(OpenCmdKeyGesture)

CommandBinding

CommandBinding 将命令与实现该命令的事件处理程序相关联。

CommandBinding 类包含 Command 属性,及 PreviewExecutedExecutedPreviewCanExecuteCanExecute 事件。

Command 是与 CommandBinding 关联的命令。 附加到 PreviewExecutedExecuted 事件的事件处理程序实现命令逻辑。 附加到 PreviewCanExecuteCanExecute 事件的事件处理程序确定是否可以在当前命令目标上执行该命令。

以下示例演示如何在应用程序的根 Window 上创建 CommandBindingCommandBindingOpen 命令与 ExecutedCanExecute 处理程序关联。

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

接下来,创建了 ExecutedRoutedEventHandlerCanExecuteRoutedEventHandlerExecutedRoutedEventHandler 打开了显示字符串的 MessageBox,该字符串表示已执行此命令。 CanExecuteRoutedEventHandlerCanExecute 属性设置为 true

void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}
Private Sub OpenCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim command, targetobj As String
    command = CType(e.Command, RoutedCommand).Name
    targetobj = CType(sender, FrameworkElement).Name
    MessageBox.Show("The " + command + " command has been invoked on target object " + targetobj)
End Sub
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}
Private Sub OpenCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    e.CanExecute = True
End Sub

CommandBinding 附加到特定对象,例如应用程序或控件的根 WindowCommandBinding 附加到的对象定义了绑定的范围。 例如,附加到命令目标的上级元素的 CommandBinding 可以通过 Executed 事件到达,但无法到达附加到命令目标的下级元素的 CommandBinding。 其直接原因在于 RoutedEvent 从引发事件的对象通行和浮升的方式。

在某些情况下,CommandBinding 会附加到命令目标本身,例如 TextBox 类及 CutCopyPaste 命令。 然而,很多时候将 CommandBinding 附加到命令目标的上级元素(例如主要 Window 或应用程序对象)会更加方便,尤其是在同一 CommandBinding 可用于多个命令目标时。 这是在创建命令基础结构时需要考虑的设计决策。

命令目标

命令目标是在其上执行命令的元素。 关于 RoutedCommand,命令目标是 ExecutedCanExecute 的路由开始的元素。 如前所述,在 WPF 中,仅当 ICommandRoutedCommand 时,ICommandSource 上的 CommandTarget 属性才适用。 如果在 ICommandSource 上设置 CommandTarget 并且相应的命令不是 RoutedCommand,则忽略命令目标。

命令源可以显式设置命令目标。 如果未定义命令目标,则具有键盘焦点的元素将用作命令目标。 将具有键盘焦点的元素用作命令目标的一个好处在于,这样可使应用程序开发者能够使用同一命令源在多个目标上调用命令,而无需跟踪命令目标。 例如,如果 MenuItem 在具有 TextBox 控件和 PasswordBox 控件的应用程序中调用“Paste”命令,则目标可以是 TextBoxPasswordBox,具体取决于哪个控件具有键盘焦点

以下示例演示如何在标记和代码隐藏中显式设置命令目标。

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste"
              CommandTarget="{Binding ElementName=mainTextBox}" />
  </Menu>
  <TextBox Name="mainTextBox"/>
</StackPanel>
// Creating the UI objects
StackPanel mainStackPanel = new StackPanel();
TextBox pasteTextBox = new TextBox();
Menu stackPanelMenu = new Menu();
MenuItem pasteMenuItem = new MenuItem();

// Adding objects to the panel and the menu
stackPanelMenu.Items.Add(pasteMenuItem);
mainStackPanel.Children.Add(stackPanelMenu);
mainStackPanel.Children.Add(pasteTextBox);

// Setting the command to the Paste command
pasteMenuItem.Command = ApplicationCommands.Paste;

// Setting the command target to the TextBox
pasteMenuItem.CommandTarget = pasteTextBox;
' Creating the UI objects
Dim mainStackPanel As New StackPanel()
Dim pasteTextBox As New TextBox()
Dim stackPanelMenu As New Menu()
Dim pasteMenuItem As New MenuItem()

' Adding objects to the panel and the menu
stackPanelMenu.Items.Add(pasteMenuItem)
mainStackPanel.Children.Add(stackPanelMenu)
mainStackPanel.Children.Add(pasteTextBox)

' Setting the command to the Paste command
pasteMenuItem.Command = ApplicationCommands.Paste

CommandManager

CommandManager 提供许多与命令相关的函数。 它提供了一组静态方法,用于在特定元素中添加和删除 PreviewExecutedExecutedPreviewCanExecuteCanExecute 事件处理程序。 它提供了将 CommandBindingInputBinding 对象注册到特定类的方法。 CommandManager 还通过 RequerySuggested 事件提供了一种方法,用于在应引发 CanExecuteChanged 事件时通知命令。

InvalidateRequerySuggested 方法强制 CommandManager 引发 RequerySuggested 事件。 这在应禁用/启用命令的情况下非常有用,但对于 CommandManager 可识别的情况,则不太有用。

命令库

WPF 提供一组预定义命令。 命令库包括以下类:ApplicationCommandsNavigationCommandsMediaCommandsEditingCommandsComponentCommands。 这些类提供诸如 CutBrowseBackBrowseForwardPlayStopPause 的命令。

许多这些命令都包含一组默认输入绑定。 例如,如果指定应用程序处理复制命令,则可自动获取键盘绑定“CTRL+C”。此外,还可获得其他输入设备的绑定,例如 Tablet PC 笔势和语音信息。

使用 XAML 引用各个命令库中的命令时,通常可省略公开静态命令属性的库类的类名。 一般来说,命令名称是明确作为字符串的,且存在所属类型来提供命令的逻辑分组,不过对于消除二义性这并不必要。 例如,可指定 Command="Cut" 而不是更为冗长的 Command="ApplicationCommands.Cut"。 这是针对命令内置于 WPF XAML 处理器中的便捷机制(更准确地说,它是 WPF XAML 处理器在加载时所引用的 ICommand 的类型转换器行为)。

创建自定义命令

如果命令库类中的命令不能满足需要,你可以创建自己的命令。 可通过两种方式创建自定义命令。 第一种方式是从头开始并实现 ICommand 接口。 另一种更常见的方法是创建 RoutedCommandRoutedUICommand

有关创建自定义 RoutedCommand 的示例,请参阅创建自定义 RoutedCommand 示例

另请参阅