Table of Contents

Class AsyncRelayCommand

Namespace
CommunityToolkit.Mvvm.Input
Assembly
CommunityToolkit.Mvvm.dll

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.

public sealed class AsyncRelayCommand : IAsyncRelayCommand, IRelayCommand, ICommand, INotifyPropertyChanged
Inheritance
AsyncRelayCommand
Implements
Inherited Members
Extension Methods

Constructors

AsyncRelayCommand(Func<CancellationToken, Task>)

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute)

Parameters

cancelableExecute Func<CancellationToken, Task>

The cancelable execution logic.

Exceptions

ArgumentNullException

Thrown if cancelableExecute is null.

AsyncRelayCommand(Func<CancellationToken, Task>, AsyncRelayCommandOptions)

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)

Parameters

cancelableExecute Func<CancellationToken, Task>

The cancelable execution logic.

options AsyncRelayCommandOptions

The options to use to configure the async command.

Exceptions

ArgumentNullException

Thrown if cancelableExecute is null.

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

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, Func<bool> canExecute)

Parameters

cancelableExecute Func<CancellationToken, Task>

The cancelable execution logic.

canExecute Func<bool>

The execution status logic.

Exceptions

ArgumentNullException

Thrown if cancelableExecute or canExecute are null.

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

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, Func<bool> canExecute, AsyncRelayCommandOptions options)

Parameters

cancelableExecute Func<CancellationToken, Task>

The cancelable execution logic.

canExecute Func<bool>

The execution status logic.

options AsyncRelayCommandOptions

The options to use to configure the async command.

Exceptions

ArgumentNullException

Thrown if cancelableExecute or canExecute are null.

AsyncRelayCommand(Func<Task>)

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<Task> execute)

Parameters

execute Func<Task>

The execution logic.

Exceptions

ArgumentNullException

Thrown if execute is null.

AsyncRelayCommand(Func<Task>, AsyncRelayCommandOptions)

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<Task> execute, AsyncRelayCommandOptions options)

Parameters

execute Func<Task>

The execution logic.

options AsyncRelayCommandOptions

The options to use to configure the async command.

Exceptions

ArgumentNullException

Thrown if execute is null.

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

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<Task> execute, Func<bool> canExecute)

Parameters

execute Func<Task>

The execution logic.

canExecute Func<bool>

The execution status logic.

Exceptions

ArgumentNullException

Thrown if execute or canExecute are null.

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

Initializes a new instance of the AsyncRelayCommand class.

public AsyncRelayCommand(Func<Task> execute, Func<bool> canExecute, AsyncRelayCommandOptions options)

Parameters

execute Func<Task>

The execution logic.

canExecute Func<bool>

The execution status logic.

options AsyncRelayCommandOptions

The options to use to configure the async command.

Exceptions

ArgumentNullException

Thrown if execute or canExecute are null.

Properties

CanBeCanceled

public bool CanBeCanceled { get; }

Property Value

bool

ExecutionTask

public Task? ExecutionTask { get; }

Property Value

Task

IsCancellationRequested

public bool IsCancellationRequested { get; }

Property Value

bool

IsRunning

public bool IsRunning { get; }

Property Value

bool

Methods

CanExecute(object?)

public bool CanExecute(object? parameter)

Parameters

parameter object

Returns

bool

Cancel()

public void Cancel()

Execute(object?)

public void Execute(object? parameter)

Parameters

parameter object

ExecuteAsync(object?)

public Task ExecuteAsync(object? parameter)

Parameters

parameter object

Returns

Task

NotifyCanExecuteChanged()

public void NotifyCanExecuteChanged()

Events

CanExecuteChanged

public event EventHandler? CanExecuteChanged

Event Type

EventHandler

PropertyChanged

public event PropertyChangedEventHandler? PropertyChanged

Event Type

PropertyChangedEventHandler