Table of Contents

Class RelayCommand<T>

Namespace
CommunityToolkit.Mvvm.Input
Assembly
CommunityToolkit.Mvvm.dll

A generic command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is true. This class allows you to accept command parameters in the Execute(T?) and CanExecute(T?) callback methods.

public sealed class RelayCommand<T> : IRelayCommand<T>, IRelayCommand, ICommand

Type Parameters

T

The type of parameter being passed as input to the callbacks.

Inheritance
RelayCommand<T>
Implements
Inherited Members

Constructors

RelayCommand(Action<T?>)

Initializes a new instance of the RelayCommand<T> class that can always execute.

public RelayCommand(Action<T?> execute)

Parameters

execute Action<T>

The execution logic.

Remarks

Due to the fact that the ICommand interface exposes methods that accept a nullable object parameter, it is recommended that if T is a reference type, you should always declare it as nullable, and to always perform checks within execute.

Exceptions

ArgumentNullException

Thrown if execute is null.

RelayCommand(Action<T?>, Predicate<T?>)

Initializes a new instance of the RelayCommand<T> class.

public RelayCommand(Action<T?> execute, Predicate<T?> canExecute)

Parameters

execute Action<T>

The execution logic.

canExecute Predicate<T>

The execution status logic.

Remarks

Exceptions

ArgumentNullException

Thrown if execute or canExecute are null.

Methods

CanExecute(object?)

public bool CanExecute(object? parameter)

Parameters

parameter object

Returns

bool

CanExecute(T?)

public bool CanExecute(T? parameter)

Parameters

parameter T

Returns

bool

Execute(object?)

public void Execute(object? parameter)

Parameters

parameter object

Execute(T?)

public void Execute(T? parameter)

Parameters

parameter T

NotifyCanExecuteChanged()

public void NotifyCanExecuteChanged()

Events

CanExecuteChanged

public event EventHandler? CanExecuteChanged

Event Type

EventHandler