Class ExceptionHandlingAsyncRelayCommand
An async relay command that handles exceptions by showing them to the user. It wraps an async action and awaits it when the command is invoked. If the action throws, the exception is caught and shown via IMessageManager.ShowMessage(). While the action is running, IsRunning is true and (by default) the command cannot be executed again.
Inherited Members
Namespace: Benday.Presentation
Assembly: Benday.Presentation.Core.dll
Syntax
public class ExceptionHandlingAsyncRelayCommand : IAsyncRelayCommand, IRelayCommand, ICommand, INotifyPropertyChanged
Constructors
| Edit this page View SourceExceptionHandlingAsyncRelayCommand(IMessageManager, Func<Task>)
Creates a new instance of the ExceptionHandlingAsyncRelayCommand class.
Declaration
public ExceptionHandlingAsyncRelayCommand(IMessageManager msgManager, Func<Task> execute)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessageManager | msgManager | Instance of IMessageManager to use |
| Func<Task> | execute | The async action to perform |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
ExceptionHandlingAsyncRelayCommand(IMessageManager, Func<Task>, Func<bool>?)
Creates a new instance of the ExceptionHandlingAsyncRelayCommand class that can be enabled and disabled.
Declaration
public ExceptionHandlingAsyncRelayCommand(IMessageManager msgManager, Func<Task> execute, Func<bool>? canExecute)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessageManager | msgManager | Instance of IMessageManager to use |
| Func<Task> | execute | The async action to perform |
| Func<bool> | canExecute | Returns true when the command can run. Call NotifyCanExecuteChanged() when the answer changes. Null means whenever the action is not already running. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
Properties
| Edit this page View SourceCanBeCanceled
Always false: the wrapped action does not take a cancellation token.
Declaration
public bool CanBeCanceled { get; }
Property Value
| Type | Description |
|---|---|
| bool |
ExecutionTask
The task for the most recent execution, if any.
Declaration
public Task? ExecutionTask { get; }
Property Value
| Type | Description |
|---|---|
| Task |
IsCancellationRequested
Always false: the wrapped action does not take a cancellation token.
Declaration
public bool IsCancellationRequested { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsRunning
True while the action is running.
Declaration
public bool IsRunning { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
| Edit this page View SourceCanExecute(object?)
Returns true if the command can be executed. False while the action is running.
Declaration
public bool CanExecute(object? parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| object | parameter |
Returns
| Type | Description |
|---|---|
| bool |
Cancel()
Does nothing: the wrapped action does not take a cancellation token.
Declaration
public void Cancel()
Execute(object?)
Starts the action without waiting for it. Exceptions are shown to the user, never thrown.
Declaration
public void Execute(object? parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| object | parameter |
ExecuteAsync(object?)
Runs the action and returns a task that completes when it has finished, after any exception has been shown to the user. The returned task does not fault.
Declaration
public Task ExecuteAsync(object? parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| object | parameter |
Returns
| Type | Description |
|---|---|
| Task |
NotifyCanExecuteChanged()
Raises CanExecuteChanged.
Declaration
public void NotifyCanExecuteChanged()
Events
| Edit this page View SourceCanExecuteChanged
Raised when the answer from CanExecute(object?) may have changed, including when the action starts and finishes.
Declaration
public event EventHandler? CanExecuteChanged
Event Type
| Type | Description |
|---|---|
| EventHandler |
PropertyChanged
Raised when IsRunning or ExecutionTask change.
Declaration
public event PropertyChangedEventHandler? PropertyChanged
Event Type
| Type | Description |
|---|---|
| PropertyChangedEventHandler |