docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class AsyncRelayCommand

    A command that mirrors the functionality of RelayCommand, with the addition of accepting a Func<TResult> returning a Task as the execute action, and providing an executionTask property that notifies changes when ExecuteAsync(object?) is invoked and when the returned Task completes.

    Inheritance
    object
    AsyncRelayCommand
    Implements
    IAsyncRelayCommand
    IRelayCommand
    ICommand
    INotifyPropertyChanged
    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 : IAsyncRelayCommand, IRelayCommand, ICommand, INotifyPropertyChanged

    Constructors

    AsyncRelayCommand(Func<CancellationToken, Task>)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<CancellationToken, Task>, Func<bool>)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Func<bool> canExecute

    The Func<TResult> to invoke when CanExecute() is called.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<CancellationToken, Task>, Func<bool>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Func<bool> canExecute

    The Func<TResult> to invoke when CanExecute() is called.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<CancellationToken, Task>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<Task>)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<Task>, Func<bool>)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Func<bool> canExecute

    The Func<TResult> to invoke when CanExecute() is called.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<Task>, Func<bool>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    Func<bool> canExecute

    The Func<TResult> to invoke when CanExecute() is called.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<Task>, AsyncRelayCommandOptions)

    Initializes a new instance of the AsyncRelayCommand class.

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

    The Func<TResult> to invoke when ExecuteAsync(object?) is called.

    AsyncRelayCommandOptions options

    The AsyncRelayCommandOptions to use.

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    Fields

    CanBeCanceledChangedEventArgs

    The arguments for the PropertyChanged event raised by the canBeCancelled property.

    Declaration
    public static readonly PropertyChangedEventArgs CanBeCanceledChangedEventArgs
    Field Value
    Type Description
    PropertyChangedEventArgs

    ExecutionTaskChangedEventArgs

    The arguments for the PropertyChanged event raised by the executionTask property.

    Declaration
    public static readonly PropertyChangedEventArgs ExecutionTaskChangedEventArgs
    Field Value
    Type Description
    PropertyChangedEventArgs

    IsCancellationRequestedChangedEventArgs

    The arguments for the PropertyChanged event raised by the isCancellationRequested property.

    Declaration
    public static readonly PropertyChangedEventArgs IsCancellationRequestedChangedEventArgs
    Field Value
    Type Description
    PropertyChangedEventArgs

    IsRunningChangedEventArgs

    The arguments for the PropertyChanged event raised by the isRunning property.

    Declaration
    public static readonly PropertyChangedEventArgs IsRunningChangedEventArgs
    Field Value
    Type Description
    PropertyChangedEventArgs

    Properties

    canBeCancelled

    Gets the CancellationTokenSource that can be used to cancel the asynchronous operation.

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

    executionTask

    Gets the Task that represents the asynchronous operation.

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

    isCancellationRequested

    Gets the CancellationTokenSource that can be used to cancel the asynchronous operation.

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

    isRunning

    Gets the CancellationTokenSource that can be used to cancel the asynchronous operation.

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

    Methods

    CanExecute()

    Determines whether this AsyncRelayCommand can execute in its current state.

    Declaration
    public bool CanExecute()
    Returns
    Type Description
    bool

    true if this AsyncRelayCommand can execute; otherwise, false.

    CanExecute(object?)

    Determines whether this AsyncRelayCommand 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 this AsyncRelayCommand can execute.

    Returns
    Type Description
    bool

    true if this AsyncRelayCommand can execute; otherwise, false.

    Cancel()

    Cancels the AsyncRelayCommand on the current command target.

    Declaration
    public void Cancel()

    Execute()

    Executes the AsyncRelayCommand on the current command target.

    Declaration
    public void Execute()

    Execute(object?)

    Executes the AsyncRelayCommand on the current command target.

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

    The parameter to use when executing the AsyncRelayCommand.

    ExecuteAsync(object?)

    Executes the AsyncRelayCommand on the current command target.

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

    The parameter to use when executing the AsyncRelayCommand.

    Returns
    Type Description
    Task

    The Task that represents the asynchronous operation.

    NotifyCanExecuteChanged()

    Raises the CanExecuteChanged event.

    Declaration
    public void NotifyCanExecuteChanged()

    Events

    CanExecuteChanged

    Event that is raised when changes occur that affect whether or not the AsyncRelayCommand should execute.

    Declaration
    public event EventHandler? CanExecuteChanged
    Event Type
    Type Description
    EventHandler

    PropertyChanged

    Event that is raised when the executionTask property changes.

    Declaration
    public event PropertyChangedEventHandler? PropertyChanged
    Event Type
    Type Description
    PropertyChangedEventHandler

    Implements

    IAsyncRelayCommand
    IRelayCommand
    ICommand
    INotifyPropertyChanged
    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)