ICommand 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義互動式 UI 元素的命令列為,以在叫用時執行動作,例如傳送電子郵件、刪除專案或提交表單。
public interface class ICommand
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(3853464898, 51815, 16513, 153, 91, 112, 157, 209, 55, 146, 223)]
struct ICommand
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.Guid(3853464898, 51815, 16513, 153, 91, 112, 157, 209, 55, 146, 223)]
public interface ICommand
Public Interface ICommand
- 衍生
- 屬性
範例
在這裡,我們會定義只將其功能轉寄給其他物件的命令。
如需完整的應用程式,請參閱 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 控制項,當使用者按一下該控制項時會叫用命令。 有兩種方式可以管理命令體驗:
-
Click
處理事件 - 將
Command
屬性系結至ICommand
描述命令邏輯的實作
方法
CanExecute(Object) |
擷取命令是否可以在其目前狀態中執行。 |
Execute(Object) |
定義叫用命令時要呼叫的方法。 |
事件
CanExecuteChanged |
每當發生會影響命令是否可以執行的動作時發生。 |