语言

ICommand 接口

定义

定义命令。

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

注解

RoutedCommand 是 RoutedUICommand Windows Presentation Foundation(WPF)中接口的 ICommand 两个实现。

Windows 运行时应用中的 ICommand

该 ICommand 接口是用于在 .NET 中为 Windows 运行时应用编写的命令的代码协定。 这些命令为 UI 元素(例如 Windows 运行时 XAML Button )提供命令行为,尤其是 AppBarButton。 如果要为 Windows 运行时应用定义命令,则使用基本相同的技术来定义 .NET 应用的命令。 通过定义实现 ICommand 和具体实现方法的 Execute 类来实现命令。

适用于 Windows 运行时的 XAML 不支持 x:Static,因此,如果从 Windows 运行时 XAML 使用命令,请不要尝试使用 x:Static 标记扩展。 此外,Windows 运行时没有任何预定义的命令库,因此此处所示的 XAML 语法并不真正适用于实现接口并定义用于 Windows 运行时用法的命令的情况。

XAML 属性用法

<object-property="predefined-command-name"/>
-or-
<object-property="predefined-class-name.predefined-command-name"/>
-or-
<object-property="{custom-class-name.custom-command-name}"/>

XAML 值

predefined-class-name
预定义命令类之一。

predefined-command-name
预定义命令之一。

custom-class-name
包含自定义命令的自定义类。 自定义类通常需要 xlmns 前缀映射。 有关详细信息,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射。

custom-command-name
自定义命令。

方法

名称 说明
CanExecute(Object)

确定命令是否可以在其当前状态下执行。

Execute(Object)

定义调用命令时要调用的方法。

事件

名称 说明
CanExecuteChanged

发生影响命令是否应执行的更改时发生。

适用于