docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class AsyncRelayCommand<T>

    A generic command that provides a more specific version of AsyncRelayCommand.

    Inheritance
    object
    AsyncRelayCommand<T>
    Implements
    IAsyncRelayCommand<T>
    IAsyncRelayCommand
    INotifyPropertyChanged
    IRelayCommand<T>
    IRelayCommand
    ICommand
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.AppUI.MVVM
    Assembly: Unity.AppUI.MVVM.dll
    Syntax
    public class AsyncRelayCommand<T> : IAsyncRelayCommand<T>, IAsyncRelayCommand, INotifyPropertyChanged, IRelayCommand<T>, IRelayCommand, ICommand
    Type Parameters
    Name Description
    T

    The type of parameter being passed as input to the callbacks.

    Constructors

    AsyncRelayCommand(Func<T?, CancellationToken, Task>)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancellableExecute)
    Parameters
    Type Name Description
    Func<T, CancellationToken, Task> cancellableExecute

    The Func<T1, T2, TResult> to invoke when the command is executed.

    Exceptions
    Type Condition
    ArgumentNullException

    cancellableExecute is null.

    AsyncRelayCommand(Func<T?, CancellationToken, Task>, Predicate<T?>)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancellableExecute, Predicate<T?> canExecute)
    Parameters
    Type Name Description
    Func<T, CancellationToken, Task> cancellableExecute

    The Func<T1, T2, TResult> to invoke when the command is executed.

    Predicate<T> canExecute

    The Predicate<T> to invoke to determine whether the command can execute.

    Exceptions
    Type Condition
    ArgumentNullException

    cancellableExecute or canExecute is null.

    AsyncRelayCommand(Func<T?, CancellationToken, Task>, Predicate<T?>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancellableExecute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
    Parameters
    Type Name Description
    Func<T, CancellationToken, Task> cancellableExecute

    The Func<T1, T2, TResult> to invoke when the command is executed.

    Predicate<T> canExecute

    The Predicate<T> to invoke to determine whether the command can execute.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use when executing the command.

    Exceptions
    Type Condition
    ArgumentNullException

    cancellableExecute or canExecute is null.

    AsyncRelayCommand(Func<T?, CancellationToken, Task>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancellableExecute, AsyncRelayCommandOptions options)
    Parameters
    Type Name Description
    Func<T, CancellationToken, Task> cancellableExecute

    The Func<T1, T2, TResult> to invoke when the command is executed.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use when executing the command.

    Exceptions
    Type Condition
    ArgumentNullException

    cancellableExecute is null.

    AsyncRelayCommand(Func<T?, Task>)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, Task> execute)
    Parameters
    Type Name Description
    Func<T, Task> execute

    The Func<T, TResult> to invoke when the command is executed.

    Exceptions
    Type Condition
    ArgumentNullException

    execute is null.

    AsyncRelayCommand(Func<T?, Task>, Predicate<T?>)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute)
    Parameters
    Type Name Description
    Func<T, Task> execute

    The Func<T, TResult> to invoke when the command is executed.

    Predicate<T> canExecute

    The Predicate<T> to invoke to determine whether the command can execute.

    Exceptions
    Type Condition
    ArgumentNullException

    execute or canExecute is null.

    AsyncRelayCommand(Func<T?, Task>, Predicate<T?>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute, AsyncRelayCommandOptions options)
    Parameters
    Type Name Description
    Func<T, Task> execute

    The Func<T1, T2, TResult> to invoke when the command is executed.

    Predicate<T> canExecute

    The Predicate<T> to invoke to determine whether the command can execute.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use when executing the command.

    Exceptions
    Type Condition
    ArgumentNullException

    execute or canExecute is null.

    AsyncRelayCommand(Func<T?, Task>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand<T> class.

    Declaration
    public AsyncRelayCommand(Func<T?, Task> execute, AsyncRelayCommandOptions options)
    Parameters
    Type Name Description
    Func<T, Task> execute

    The Func<T, TResult> to invoke when the command is executed.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use when executing the command.

    Exceptions
    Type Condition
    ArgumentNullException

    execute is null.

    Properties

    canBeCancelled

    Gets a value indicating whether this AsyncRelayCommand<T> can be cancelled.

    Declaration
    public bool canBeCancelled { get; }
    Property Value
    Type Description
    bool

    executionTask

    Gets a value indicating whether this AsyncRelayCommand<T> is currently running.

    Declaration
    public Task? executionTask { get; set; }
    Property Value
    Type Description
    Task

    isCancellationRequested

    Gets a value indicating whether this AsyncRelayCommand<T> has been cancelled.

    Declaration
    public bool isCancellationRequested { get; }
    Property Value
    Type Description
    bool

    isRunning

    Gets a value indicating whether this AsyncRelayCommand<T> is currently running.

    Declaration
    public bool isRunning { get; }
    Property Value
    Type Description
    bool

    Methods

    CanExecute(object?)

    Determines whether this AsyncRelayCommand<T> can execute in its current state.

    Declaration
    public bool CanExecute(object? parameter)
    Parameters
    Type Name Description
    object parameter

    The parameter to use when determining whether the command can execute.

    Returns
    Type Description
    bool

    True if this command can be executed; otherwise, false.

    Exceptions
    Type Condition
    InvalidOperationException

    ExecuteAsync(T?) has not been called.

    CanExecute(T?)

    Determines whether this AsyncRelayCommand<T> can execute in its current state.

    Declaration
    public bool CanExecute(T? parameter)
    Parameters
    Type Name Description
    T parameter

    The parameter to use when determining whether the command can execute.

    Returns
    Type Description
    bool

    True if this command can be executed; otherwise, false.

    Cancel()

    Cancels the current execution of this AsyncRelayCommand<T>.

    Declaration
    public void Cancel()

    Execute(object)

    Determines whether this AsyncRelayCommand<T> can execute in its current state.

    Declaration
    public void Execute(object parameter)
    Parameters
    Type Name Description
    object parameter

    The parameter to use when determining whether the command can execute.

    Exceptions
    Type Condition
    InvalidOperationException

    ExecuteAsync(T?) has not been called.

    Execute(T?)

    Executes the AsyncRelayCommand<T> synchronously on the current thread.

    Declaration
    public void Execute(T? parameter)
    Parameters
    Type Name Description
    T parameter

    The parameter to use when executing the command.

    ExecuteAsync(object?)

    Gets a value indicating whether this AsyncRelayCommand<T> can be executed.

    Declaration
    public Task ExecuteAsync(object? parameter)
    Parameters
    Type Name Description
    object parameter

    The parameter to use when determining whether the command can execute.

    Returns
    Type Description
    Task

    True if this command can be executed; otherwise, false.

    Exceptions
    Type Condition
    InvalidOperationException

    ExecuteAsync(T?) has not been called.

    ExecuteAsync(T?)

    Occurs when changes occur that affect whether the command should execute.

    Declaration
    public Task ExecuteAsync(T? parameter)
    Parameters
    Type Name Description
    T parameter

    The parameter to use when determining whether the command can execute.

    Returns
    Type Description
    Task

    True if this command can be executed; otherwise, false.

    NotifyCanExecuteChanged()

    Raises CanExecuteChanged.

    Declaration
    public void NotifyCanExecuteChanged()

    Events

    CanExecuteChanged

    Event raised when the CanExecute(object?) property changes.

    Declaration
    public event EventHandler? CanExecuteChanged
    Event Type
    Type Description
    EventHandler

    PropertyChanged

    Event raised when the executionTask property changes.

    Declaration
    public event PropertyChangedEventHandler? PropertyChanged
    Event Type
    Type Description
    PropertyChangedEventHandler

    Implements

    IAsyncRelayCommand<T>
    IAsyncRelayCommand
    INotifyPropertyChanged
    IRelayCommand<T>
    IRelayCommand
    ICommand
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)