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++ 또는 C#용 System.Windows.Input.ICommand에 대해 이 ICommand 인터페이스를 구현합니다(다양한 UI 속성, 메서드 및 이벤트 추가).

기본 예제는 사용자가 클릭할 때 명령을 호출하는 Button 컨트롤을 참조하세요. 명령 환경을 관리하는 방법에는 두 가지가 있습니다.

  • Click 이벤트 처리
  • 명령 논리를 설명하는 ICommand 구현에 Command 속성을 바인딩합니다.

메서드

CanExecute(Object)

명령이 현재 상태에서 실행할 수 있는지 여부를 검색합니다.

Execute(Object)

명령이 호출될 때 호출될 메서드를 정의합니다.

이벤트

CanExecuteChanged

명령이 실행할 수 있는지 여부에 영향을 주는 문제가 발생할 때마다 발생합니다.

적용 대상

추가 정보