ICommand インターフェイス

定義

電子メールの送信、アイテムの削除、フォームの送信など、呼び出されたときにアクションを実行する対話型 UI 要素のコマンド動作を定義します。

public interface class ICommand
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(3853464898, 51815, 16513, 153, 91, 112, 157, 209, 55, 146, 223)]
struct ICommand
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(3853464898, 51815, 16513, 153, 91, 112, 157, 209, 55, 146, 223)]
public interface ICommand
Public Interface ICommand
属性

Windows の要件

デバイス ファミリ
Windows 10 (10.0.10240.0 で導入)
API contract
Windows.Foundation.UniversalApiContract (v1.0 で導入)

ここでは、その機能を他のオブジェクトに中継するコマンドを定義します。

完全なアプリケーションについては、 UI の基本 (XAML) サンプル を参照してください。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace AppUIBasics.Common
{
    /// <summary>
    /// A command whose sole purpose is to relay its functionality
    /// to other objects by invoking delegates.
    /// The default return value for the CanExecute method is 'true'.
    /// RaiseCanExecuteChanged needs to be called whenever
    /// CanExecute is expected to return a different value.
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;
        /// <summary>
        /// Raised when RaiseCanExecuteChanged is called.
        /// </summary>
        public event EventHandler CanExecuteChanged;
        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }
        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }
        /// <summary>
        /// Determines whether this RelayCommand can execute in its current state.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed,
        /// this object can be set to null.
        /// </param>
        /// <returns>true if this command can be executed; otherwise, false.</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }
        /// <summary>
        /// Executes the RelayCommand on the current command target.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed,
        /// this object can be set to null.
        /// </param>
        public void Execute(object parameter)
        {
            _execute();
        }
        /// <summary>
        /// Method used to raise the CanExecuteChanged event
        /// to indicate that the return value of the CanExecute
        /// method has changed.
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
}

注釈

XamlUICommand は、C++ または System.Windows.Input.ICommand for C# 用にこの ICommand インターフェイスを実装します (さまざまな UI プロパティ、メソッド、およびイベントを追加します)。

基本的な例については、ユーザーがクリックしたときにコマンドを呼び出す Button コントロールを参照してください。 コマンド エクスペリエンスを管理するには、次の 2 つの方法があります。

  • Click イベントの処理
  • Command プロパティを、コマンド ロジックを記述する ICommand 実装にバインドする

メソッド

CanExecute(Object)

コマンドを現在の状態で実行できるかどうかを取得します。

Execute(Object)

コマンドが起動される際に呼び出すメソッドを定義します。

イベント

CanExecuteChanged

コマンドを実行できるかどうかに影響する何かが発生するたびに発生します。

適用対象

こちらもご覧ください