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
AsyncRelayCommandOptionsThe 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
orcanExecute
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
AsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
cancelableExecute
orcanExecute
are null.
AsyncRelayCommand(Func<Task>)
Initializes a new instance of the AsyncRelayCommand class.
public AsyncRelayCommand(Func<Task> execute)
Parameters
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
AsyncRelayCommandOptionsThe 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
Exceptions
- ArgumentNullException
Thrown if
execute
orcanExecute
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
AsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
execute
orcanExecute
are null.
Properties
CanBeCanceled
public bool CanBeCanceled { get; }
Property Value
ExecutionTask
public Task? ExecutionTask { get; }
Property Value
IsCancellationRequested
public bool IsCancellationRequested { get; }
Property Value
IsRunning
public bool IsRunning { get; }
Property Value
Methods
CanExecute(object?)
public bool CanExecute(object? parameter)
Parameters
parameter
object
Returns
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
NotifyCanExecuteChanged()
public void NotifyCanExecuteChanged()
Events
CanExecuteChanged
public event EventHandler? CanExecuteChanged
Event Type
PropertyChanged
public event PropertyChangedEventHandler? PropertyChanged