Edit

Share via


RelayCommandAttribute Class

Definition

An attribute that can be used to automatically generate ICommand properties from declared methods. When this attribute is used to decorate a method, a generator will create a command property with the corresponding IRelayCommand interface depending on the signature of the method. If an invalid method signature is used, the generator will report an error.

In order to use this attribute, the containing type doesn't need to implement any interfaces. The generated properties will be lazily assigned but their value will never change, so there is no need to support property change notifications or other additional functionality.

This attribute can be used as follows:

partial class MyViewModel
{
    [RelayCommand]
    private void GreetUser(User? user)
    {
        Console.WriteLine($"Hello {user.Name}!");
    }
}

And with this, code analogous to this will be generated:

partial class MyViewModel
{
    private RelayCommand? greetUserCommand;

    public IRelayCommand GreetUserCommand => greetUserCommand ??= new RelayCommand(GreetUser);
}

The following signatures are supported for annotated methods:

void Method();

Will generate an IRelayCommand property (using a RelayCommand instance).

void Method(T?);

Will generate an IRelayCommand<T> property (using a RelayCommand<T> instance).

Task Method();
Task Method(CancellationToken);
Task<T> Method();
Task<T> Method(CancellationToken);

Will both generate an IAsyncRelayCommand property (using an AsyncRelayCommand<T> instance).

Task Method(T?);
Task Method(T?, CancellationToken);
Task<T> Method(T?);
Task<T> Method(T?, CancellationToken);

Will both generate an IAsyncRelayCommand<T> property (using an AsyncRelayCommand<T> instance).

[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class RelayCommandAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type RelayCommandAttribute = class
    inherit Attribute
Public NotInheritable Class RelayCommandAttribute
Inherits Attribute
Inheritance
RelayCommandAttribute
Attributes

Constructors

RelayCommandAttribute()

Properties

AllowConcurrentExecutions

Gets or sets a value indicating whether or not to allow concurrent executions for an asynchronous command.

When set for an attribute used on a method that would result in an AsyncRelayCommand or an AsyncRelayCommand<T> property to be generated, this will modify the behavior of these commands when an execution is invoked while a previous one is still running. It is the same as creating an instance of these command types with a constructor such as AsyncRelayCommand(Func<Task>, AsyncRelayCommandOptions) and using the AllowConcurrentExecutions value.

CanExecute

Gets or sets the name of the property or method that will be invoked to check whether the generated command can be executed at any given time. The referenced member needs to return a Boolean value, and has to have a signature compatible with the target command.

FlowExceptionsToTaskScheduler

Gets or sets a value indicating whether or not to exceptions should be propagated to UnobservedTaskException.

When set for an attribute used on a method that would result in an AsyncRelayCommand or an AsyncRelayCommand<T> property to be generated, this will modify the behavior of these commands in case an exception is thrown by the underlying operation. It is the same as creating an instance of these command types with a constructor such as AsyncRelayCommand(Func<Task>, AsyncRelayCommandOptions) and using the FlowExceptionsToTaskScheduler value.

IncludeCancelCommand

Gets or sets a value indicating whether a cancel command should also be generated for an asynchronous command.

When set to true, this additional code will be generated:

partial class MyViewModel
{
    private ICommand? loginUserCancelCommand;

    public ICommand LoginUserCancelCommand => loginUserCancelCommand ??= LoginUserCommand.CreateCancelCommand();
}

Where LoginUserCommand is an IAsyncRelayCommand defined in the class (or generated by this attribute as well).

Applies to