Show / Hide Table of Contents

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.

Inheritance
object
ExceptionHandlingAsyncRelayCommand
Implements
IAsyncRelayCommand
IRelayCommand
ICommand
INotifyPropertyChanged
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: Benday.Presentation
Assembly: Benday.Presentation.Core.dll
Syntax
public class ExceptionHandlingAsyncRelayCommand : IAsyncRelayCommand, IRelayCommand, ICommand, INotifyPropertyChanged

Constructors

| Edit this page View Source

ExceptionHandlingAsyncRelayCommand(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
| Edit this page View Source

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 Source

CanBeCanceled

Always false: the wrapped action does not take a cancellation token.

Declaration
public bool CanBeCanceled { get; }
Property Value
Type Description
bool
| Edit this page View Source

ExecutionTask

The task for the most recent execution, if any.

Declaration
public Task? ExecutionTask { get; }
Property Value
Type Description
Task
| Edit this page View Source

IsCancellationRequested

Always false: the wrapped action does not take a cancellation token.

Declaration
public bool IsCancellationRequested { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsRunning

True while the action is running.

Declaration
public bool IsRunning { get; }
Property Value
Type Description
bool

Methods

| Edit this page View Source

CanExecute(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
| Edit this page View Source

Cancel()

Does nothing: the wrapped action does not take a cancellation token.

Declaration
public void Cancel()
| Edit this page View Source

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
| Edit this page View Source

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
| Edit this page View Source

NotifyCanExecuteChanged()

Raises CanExecuteChanged.

Declaration
public void NotifyCanExecuteChanged()

Events

| Edit this page View Source

CanExecuteChanged

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
| Edit this page View Source

PropertyChanged

Raised when IsRunning or ExecutionTask change.

Declaration
public event PropertyChangedEventHandler? PropertyChanged
Event Type
Type Description
PropertyChangedEventHandler

Implements

IAsyncRelayCommand
IRelayCommand
ICommand
INotifyPropertyChanged
  • Edit this page
  • View Source
In this article
Back to top Copyright © www.benday.com | info@benday.com