docs.unity3d.com
    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
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Unity.AppUI.MVVM
    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<Boolean>)

    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<Boolean> 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<Boolean>, 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<Boolean> 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<Boolean>)

    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<Boolean> canExecute

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

    Exceptions
    Type Condition
    ArgumentNullException

    If the execute argument is null.

    AsyncRelayCommand(Func<Task>, Func<Boolean>, 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<Boolean> 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
    Boolean
    Implements
    IAsyncRelayCommand.canBeCancelled

    executionTask

    Gets the Task that represents the asynchronous operation.

    Declaration
    public Task executionTask { get; set; }
    Property Value
    Type Description
    Task
    Implements
    IAsyncRelayCommand.executionTask

    isCancellationRequested

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

    Declaration
    public bool isCancellationRequested { get; }
    Property Value
    Type Description
    Boolean
    Implements
    IAsyncRelayCommand.isCancellationRequested

    isRunning

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

    Declaration
    public bool isRunning { get; }
    Property Value
    Type Description
    Boolean
    Implements
    IAsyncRelayCommand.isRunning

    Methods

    Cancel()

    Cancels the AsyncRelayCommand on the current command target.

    Declaration
    public void Cancel()
    Implements
    IAsyncRelayCommand.Cancel()

    CanExecute()

    Determines whether this AsyncRelayCommand can execute in its current state.

    Declaration
    public bool CanExecute()
    Returns
    Type Description
    Boolean

    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
    Boolean

    true if this AsyncRelayCommand can execute; otherwise, false.

    Implements
    ICommand.CanExecute(Object)

    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.

    Implements
    ICommand.Execute(Object)

    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.

    Implements
    IAsyncRelayCommand.ExecuteAsync(Object)

    NotifyCanExecuteChanged()

    Raises the CanExecuteChanged event.

    Declaration
    public void NotifyCanExecuteChanged()
    Implements
    IRelayCommand.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
    Implements
    ICommand.CanExecuteChanged

    PropertyChanged

    Event that is raised when the executionTask property changes.

    Declaration
    public event PropertyChangedEventHandler PropertyChanged
    Event Type
    Type Description
    PropertyChangedEventHandler
    Implements
    INotifyPropertyChanged.PropertyChanged
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023