Table of Contents

Simplify Commands with RelayCommands

The RelayCommands are ICommand implementations that can expose a method or delegate to the view.

ICommands Implementations

Type Description
RelayCommand Represents a command to bind a method or delegate to the view (e.g. to a Button)
ExceptionHandlingRelayCommand This command catches exceptions and show it by the MessageService
AsyncExceptionHandlingRelayCommand Use this command for long running methods, to avoid blocking the UI thread.
Tip

Use the BusyButton in the view and the AsyncExceptionHandlingRelayCommand in the view model for long running methods.
Busy Button

Example

This example shows the usage of a RelayCommand.

internal class MyViewModel
{
    public MyViewModel()
    {
        DoCommand = new AsyncExceptionHandlingRelayCommand(
            execute: _ => Do(),
            canExecute: _ => IsEnabled);
    }
    
    public ICommand DoCommand { get; }

    public bool IsEnabled { get; }

    // This method is executed by the command.
    private void Do { ... }
}