ICommand 介面


定義命令。Defines a command.

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


RoutedCommandRoutedUICommandICommandWindows 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 執行階段應用程式中的 ICommandICommand in Windows Runtime apps

ICommand介面是針對 Windows 執行階段應用程式, 以 .net 撰寫之命令的程式碼合約。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.

Windows 執行階段的 xaml 不支援 x:Static, 因此, 如果從 xaml Windows 執行階段使用此命令, 則不要嘗試x:Static使用標記延伸。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 Attribute UsageXAML Attribute Usage

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


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


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


其中一個預先定義的命令類別。One of the predefined command classes.

其中一個預先定義的命令。One of the predefined commands.

包含自訂命令的自訂類別。A custom class that contains the custom command. 自訂類別通常需要xlmns前置詞對應, 請參閱WPF xaml 的 xaml 命名空間和命名空間對應Custom classes generally require an xlmns prefix mapping; see XAML Namespaces and Namespace Mapping for WPF XAML.

自訂命令。A custom command.



定義用來判斷命令是否能以其目前狀態執行的方法。Defines the method that determines whether the command can execute in its current state.


定義叫用命令時要呼叫的方法。Defines the method to be called when the command is invoked.



發生於影響命令是否應執行的變更發生時。Occurs when changes occur that affect whether or not the command should execute.