CommunityToolkit / CommunityToolkit/dotnet

Add a Property in RelayCommandAttribute to allow to set the XXXX part of autogenerated name "XXXXCommand" instead of pick the method name.

Open
#1,172 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

Sometimes we are forced to put a name to a method (for repeated name with other methods) that it is not "ideal" to put as the generated command name. It will be preferrable to be able to set the name of the command manually.

This brings more control to the developer even to name the methods as they want.

### API breakdown

```csharp
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class RelayCommandAttribute : Attribute
{
public string? Name { get; init; } //--> If null or whitespace, default behaviour
public string? CanExecute { get; init; }
public bool AllowConcurrentExecutions { get; init; }
public bool FlowExceptionsToTaskScheduler { get; init; }
public bool IncludeCancelCommand { get; init; }
}
```

### Usage example

[RelayCommand(Name = "SaveProject", CanExecute = nameof(CanSaveProject), IncludeCancelCommand = true)]
private async Task SaveProjectPrivateAsync(T? projectViewModel, CancellationToken cancellationToken = default);
protected virtual bool CanSaveProject(T? projectViewModel);

### Breaking change?

No

### Alternatives

Nothing.

### Additional context

_No response_

### Help us help you

No, just wanted to propose this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.