ICommand ICommand ICommand ICommand Interface

定义

定义一个命令Defines a command.

public interface class ICommand
[System.ComponentModel.TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
[System.ComponentModel.TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public interface ICommand
type ICommand = interface
Public Interface ICommand
派生
属性

注解

RoutedCommandRoutedUICommand的两个实现ICommand接口中Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)RoutedCommand and RoutedUICommand are two implementations of the ICommand interface in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF).

在 Windows 中的 ICommand 运行时应用ICommand in Windows Runtime apps

ICommand接口是.NET for Windows Runtime 应用程序中编写的命令的代码协定。The ICommand interface is the code contract for commands that are written in .NET for Windows Runtime apps. 这些命令为 UI 元素,例如 Windows 运行时 XAML 提供的命令行为Button特别AppBarButtonThese commands provide the commanding behavior for UI elements such as a Windows Runtime XAML Button and in particular an AppBarButton. 如果你定义的 Windows 运行时应用的命令将基本上会使用用于定义.NET 应用的命令的相同技术。If you're defining commands for Windows Runtime apps you use basically the same techniques you'd use for defining commands for a .NET app. 通过定义实现的类中实现命令ICommand,并专门实现Execute方法。Implement the command by defining a class that implements ICommand and specifically implement the Execute method.

XAML 的 Windows 运行时不支持 x:Static,因此,请勿尝试使用x:Static如果该命令用于从 Windows 运行时 XAML 标记扩展。XAML for Windows Runtime does not support x:Static, so don't attempt to use the x:Static markup extension if the command is used from Windows Runtime XAML. 此外,Windows 运行时没有任何预定义的命令库,因此如下所示的 XAML 语法实际上并不适用的情况下,要在其中实现接口并定义适用于 Windows 运行时使用的命令。Also, the Windows Runtime does not have any predefined command libraries, so the XAML syntax shown here doesn't really apply for the case where you're implementing the interface and defining the command for Windows Runtime usage.

XAML 属性用法XAML Attribute Usage

<object property="predefinedCommandName"/><object property="predefinedCommandName"/>

- 或 --or-

<object property="predefinedCommandName.predefinedCommandName"/><object property="predefinedCommandName.predefinedCommandName"/>

- 或 --or-

<object property="{ customClassName.customCommandName}"/><object property="{ customClassName.customCommandName}"/>

XAML 值XAML Values

predefinedClassNamepredefinedClassName
预定义的命令类之一。One of the predefined command classes.

predefinedCommandNamepredefinedCommandName
预定义的命令之一。One of the predefined commands.

customClassNamecustomClassName
一个包含自定义命令的自定义类。A custom class that contains the custom command. 通常需要自定义类xlmns前缀映射; 请参阅XAML 命名空间和 WPF XAML 的 Namespace 映射Custom classes generally require an xlmns prefix mapping; see XAML Namespaces and Namespace Mapping for WPF XAML.

customCommandNamecustomCommandName
自定义命令。A custom command.

方法

CanExecute(Object) CanExecute(Object) CanExecute(Object) CanExecute(Object)

定义方法,该方法确定此命令能否在其当前状态下执行。Defines the method that determines whether the command can execute in its current state.

Execute(Object) Execute(Object) Execute(Object) Execute(Object)

定义在调用此命令时要调用的方法。Defines the method to be called when the command is invoked.

事件

CanExecuteChanged CanExecuteChanged CanExecuteChanged CanExecuteChanged

当出现影响是否应执行该命令的更改时发生。Occurs when changes occur that affect whether or not the command should execute.

适用于