Class AsyncRelayCommandCommon<TParam>
A reusable command that encapsulates the implementation of ICommand with support for async/await command delegates.
Enables instant creation of an ICommand without implementing the ICommand interface for each command.
The AsyncRelayCommandCommon<TParam> accepts asynchronous command handlers and supports data bindng to properties like IsExecuting by implementing INotifyPropertyChanged.
Call and await the ExecuteAsync(TParam) method or one of its overloads to execute the command explicitly asynchronously.
ICommand
Inheritance
Implements
Inherited Members
Namespace: BionicCode.Utilities.Net
Assembly: BionicCode.Utilities.Net.Common.dll
Syntax
public abstract class AsyncRelayCommandCommon<TParam> : AsyncRelayCommandCommon, IAsyncRelayCommandCommon<TParam>, IAsyncRelayCommandCommon, ICommand, INotifyPropertyChanged
Type Parameters
| Name | Description |
|---|---|
| TParam |
Remarks
AsyncRelayCommandCommon implements ICommand. In case the AsyncRelayCommandCommon<TParam> is executed explicitly, especially with an asynchronous command handler registered, it is highly recommended to invoke the awaitable ExecuteAsync() or its overloads instead.
Constructors
| Improve this Doc View SourceAsyncRelayCommandCommon()
Constructor.
Declaration
protected AsyncRelayCommandCommon()
AsyncRelayCommandCommon(Action)
Creates a new parameterless synchronous command that can always execute (CanExecute() will always return true).
Declaration
protected AsyncRelayCommandCommon(Action executeNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | executeNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Action, Func<Boolean>)
Creates a new parameterless synchronous command.
Declaration
protected AsyncRelayCommandCommon(Action executeNoParam, Func<bool> canExecuteNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | executeNoParam | The execution handler. |
| Func<Boolean> | canExecuteNoParam | The execution status handler. |
AsyncRelayCommandCommon(Action<TParam, CancellationToken>)
Creates a new synchronous command that can always execute (CanExecute() will always return true)
and accepts a command parameter of type TParam
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Action<TParam, CancellationToken> execute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<TParam, CancellationToken> | execute | The awaitable execution handler. |
AsyncRelayCommandCommon(Action<TParam, CancellationToken>, Predicate<TParam>)
Creates a new synchronous command that supports cancellation adn accepts a command parameter of type TParam.
Declaration
protected AsyncRelayCommandCommon(Action<TParam, CancellationToken> executeAsync, Predicate<TParam> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<TParam, CancellationToken> | executeAsync | The awaitable execution handler. |
| Predicate<TParam> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Action<TParam>)
Creates a new synchronous command that can always execute (CanExecute() will always return true)
and accepts a command parameter of type TParam.
Declaration
protected AsyncRelayCommandCommon(Action<TParam> execute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<TParam> | execute | The awaitable execution handler. |
AsyncRelayCommandCommon(Action<TParam>, Predicate<TParam>)
Creates a new synchronous command that accepts a command parameter of type TParam.
Declaration
protected AsyncRelayCommandCommon(Action<TParam> execute, Predicate<TParam> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<TParam> | execute | The execution handler. |
| Predicate<TParam> | canExecute | The execution status handler. |
AsyncRelayCommandCommon(Action<CancellationToken>)
Creates a new parameterless synchronous command that can always execute (CanExecute() will always return true)
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Action<CancellationToken> executeNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<CancellationToken> | executeNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Action<CancellationToken>, Func<Boolean>)
Creates a new parameterless synchronous command that supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Action<CancellationToken> executeAsync, Func<bool> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<CancellationToken> | executeAsync | The awaitable execution handler. |
| Func<Boolean> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<TParam, CancellationToken, Task>)
Creates a new asynchronous command that can always execute (CanExecute() will always return true)
and that accepts a command parameter of type TParam
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Func<TParam, CancellationToken, Task> executeAsync)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<TParam, CancellationToken, Task> | executeAsync | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<TParam, CancellationToken, Task>, Predicate<TParam>)
Creates a new asynchronous command that supports cancellation and accepts a command parameter of TParam.
Declaration
protected AsyncRelayCommandCommon(Func<TParam, CancellationToken, Task> executeAsync, Predicate<TParam> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<TParam, CancellationToken, Task> | executeAsync | The awaitable execution handler. |
| Predicate<TParam> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<TParam, Task>)
Creates a new asynchronous command that can always execute (CanExecute() will always return true)
and that accepts a command parameter of type TParam.
Declaration
protected AsyncRelayCommandCommon(Func<TParam, Task> executeAsync)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<TParam, Task> | executeAsync | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<TParam, Task>, Predicate<TParam>)
Creates a new asynchronous command that accepts a command parameter of type TParam.
Declaration
protected AsyncRelayCommandCommon(Func<TParam, Task> executeAsync, Predicate<TParam> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<TParam, Task> | executeAsync | The awaitable execution handler. |
| Predicate<TParam> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<CancellationToken, Task>)
Creates a new parameterless asynchronous command that can always execute (CanExecute() will always return true)
and supports cancellation.
Declaration
protected AsyncRelayCommandCommon(Func<CancellationToken, Task> executeAsyncNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task> | executeAsyncNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<CancellationToken, Task>, Func<Boolean>)
Creates a new parameterless asynchronous command that supports cancellation and does not take a command parameter.
Declaration
protected AsyncRelayCommandCommon(Func<CancellationToken, Task> executeAsync, Func<bool> canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<CancellationToken, Task> | executeAsync | The awaitable execution handler. |
| Func<Boolean> | canExecute | The can execute handler. |
AsyncRelayCommandCommon(Func<Task>)
Creates a new parameterless asynchronous command that can always execute (CanExecute() will always return true).
Declaration
protected AsyncRelayCommandCommon(Func<Task> executeAsyncNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | executeAsyncNoParam | The awaitable execution handler. |
AsyncRelayCommandCommon(Func<Task>, Func<Boolean>)
Creates a parameterless new asynchronous command.
Declaration
protected AsyncRelayCommandCommon(Func<Task> executeAsyncNoParam, Func<bool> canExecuteNoParam)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | executeAsyncNoParam | The awaitable execution handler. |
| Func<Boolean> | canExecuteNoParam | The execution status handler. |
Properties
| Improve this Doc View SourceCanExecuteDelegate
The registered CanExecute delegate that accepts a parameter of TParam.
Declaration
protected Func<TParam, bool> CanExecuteDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<TParam, Boolean> |
|
ExecuteAsyncDelegate
The registered async execute delegate that accepts a parameter of TParam.
Declaration
protected Func<TParam, Task> ExecuteAsyncDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<TParam, Task> | A delegate that takes a command parameter of |
ExecuteCancellableAsyncDelegate
The registered async execute delegate that supports cancellation and accepts a parameter of TParam.
Declaration
protected Func<TParam, CancellationToken, Task> ExecuteCancellableAsyncDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Func<TParam, CancellationToken, Task> | A delegate that supports cancellation and takes a command parameter of |
ExecuteCancellableDelegate
The registered execute delegate that accepts a parameter of TParam.
Declaration
protected Action<TParam, CancellationToken> ExecuteCancellableDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Action<TParam, CancellationToken> | A delegate that suppoprts cancellation and takes a command parameter of |
ExecuteDelegate
The registered execute delegate that accepts a parameter of TParam.
Declaration
protected Action<TParam> ExecuteDelegate { get; }
Property Value
| Type | Description |
|---|---|
| Action<TParam> | A delegate that takes no parameter and returns a Task. |
Methods
| Improve this Doc View SourceCanExecute()
Determines whether this AsyncRelayCommandCommon can execute.
Declaration
public bool CanExecute()
Returns
| Type | Description |
|---|---|
| Boolean |
|
CanExecute(TParam)
Determines whether this AsyncRelayCommandCommon can execute.
Declaration
public bool CanExecute(TParam parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| TParam | parameter | Data used by the command. |
Returns
| Type | Description |
|---|---|
| Boolean |
|
ExecuteAsync(TParam)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(TParam parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| TParam | parameter | Data used by the command. If the command does not require data to be passed, this object can be set to null. |
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
ExecuteAsync(TParam, CancellationToken)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(TParam parameter, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| TParam | parameter | Data used by the command. If the command does not require data to be passed, this object can be set to null. |
| CancellationToken | cancellationToken | An instance of CancellationToken to cancel the executing command delegate. |
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
ExecuteAsync(TParam, TimeSpan)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(TParam parameter, TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| TParam | parameter | Data used by the command. If the command does not require data to be passed, this object can be set to null. |
| TimeSpan | timeout | A TimeSpan to specify the timeout of the operation.
|
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
| ArgumentOutOfRangeException |
|
ExecuteAsync(TParam, TimeSpan, CancellationToken)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public async Task ExecuteAsync(TParam parameter, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| TParam | parameter | Data used by the command. If the command does not require data to be passed, this object can be set to null. |
| TimeSpan | timeout | A TimeSpan to specify the timeout of the operation.
|
| CancellationToken | cancellationToken | An instance of CancellationToken to cancel the executing command delegate. |
Returns
| Type | Description |
|---|---|
| Task |
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
| ArgumentOutOfRangeException |
|
ExecuteAsync(TimeSpan, CancellationToken)
Executes the AsyncRelayCommand on the current command target asynchronously.
Declaration
public override async Task ExecuteAsync(TimeSpan timeout, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | A TimeSpan to specify the timeout of the operation.
|
| CancellationToken | cancellationToken | An instance of CancellationToken to cancel the executing command delegate. |
Returns
| Type | Description |
|---|---|
| Task |
Overrides
Remarks
If the registered command handler is asynchronous (awaitable), then the execution is asynchronous otherwise the delegate is executed synchronously.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | If the executing command delegate was cancelled. |
| ArgumentOutOfRangeException |
|
Explicit Interface Implementations
| Improve this Doc View SourceICommand.CanExecute(Object)
Declaration
bool ICommand.CanExecute(object parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | parameter |
Returns
| Type | Description |
|---|---|
| Boolean |
ICommand.Execute(Object)
Declaration
async void ICommand.Execute(object parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | parameter |