Bearbeiten

Freigeben über


ICommandAttribute 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
{
    [ICommand]
    private void GreetUser(User? user)
    {
        Console.WriteLine($"Hello {user.Name}!");
    }
}

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

partial class MyViewModel
{
    private IRelayCommand? 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);

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

Task Method(T?);
Task 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 ICommandAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type ICommandAttribute = class
    inherit Attribute
Public NotInheritable Class ICommandAttribute
Inherits Attribute
Inheritance
ICommandAttribute
Attributes

Constructors

ICommandAttribute()

Applies to