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
cancelableExecuteFunc<CancellationToken, Task>The cancelable execution logic.
Exceptions
- ArgumentNullException
Thrown if
cancelableExecuteis null.
AsyncRelayCommand(Func<CancellationToken, Task>, AsyncRelayCommandOptions)
Initializes a new instance of the AsyncRelayCommand class.
public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, AsyncRelayCommandOptions options)
Parameters
cancelableExecuteFunc<CancellationToken, Task>The cancelable execution logic.
optionsAsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
cancelableExecuteis 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
cancelableExecuteFunc<CancellationToken, Task>The cancelable execution logic.
canExecuteFunc<bool>The execution status logic.
Exceptions
- ArgumentNullException
Thrown if
cancelableExecuteorcanExecuteare 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
cancelableExecuteFunc<CancellationToken, Task>The cancelable execution logic.
canExecuteFunc<bool>The execution status logic.
optionsAsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
cancelableExecuteorcanExecuteare null.
AsyncRelayCommand(Func<Task>)
Initializes a new instance of the AsyncRelayCommand class.
public AsyncRelayCommand(Func<Task> execute)
Parameters
Exceptions
- ArgumentNullException
Thrown if
executeis null.
AsyncRelayCommand(Func<Task>, AsyncRelayCommandOptions)
Initializes a new instance of the AsyncRelayCommand class.
public AsyncRelayCommand(Func<Task> execute, AsyncRelayCommandOptions options)
Parameters
executeFunc<Task>The execution logic.
optionsAsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
executeis 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
executeorcanExecuteare 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
executeFunc<Task>The execution logic.
canExecuteFunc<bool>The execution status logic.
optionsAsyncRelayCommandOptionsThe options to use to configure the async command.
Exceptions
- ArgumentNullException
Thrown if
executeorcanExecuteare 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
parameterobject
Returns
Cancel()
public void Cancel()
Execute(object?)
public void Execute(object? parameter)
Parameters
parameterobject
ExecuteAsync(object?)
public Task ExecuteAsync(object? parameter)
Parameters
parameterobject
Returns
NotifyCanExecuteChanged()
public void NotifyCanExecuteChanged()
Events
CanExecuteChanged
public event EventHandler? CanExecuteChanged
Event Type
PropertyChanged
public event PropertyChangedEventHandler? PropertyChanged