[DelegateCommand]
Generates DelegateCommand / DelegateCommand<T> or AsyncDelegateCommand / AsyncDelegateCommand<T> from instance methods.
Supported execute shapes (summary)
voidwith zero or one parameter →DelegateCommand/DelegateCommand<T>Task(non-generic),Task<TResult>,ValueTask,ValueTask<TResult>→AsyncDelegateCommand(ValueTaskfamily via.AsTask();Task<TResult>viaasynclambda that awaits execute)CanExecute = nameof(...)→ bool method or compatible delegate (PSG2003, PSG2006 when invalid)
CancellationToken + ValueTask / ValueTask<TResult> / Task<TResult> is not supported (PSG1001).
Example
csharp
public partial class MainViewModel : BindableBase
{
[DelegateCommand]
private void Increment() { }
[DelegateCommand]
private async Task LoadAsync() { }
[DelegateCommand(CanExecute = nameof(CanSubmit))]
private void Submit() { }
private bool CanSubmit() => true;
}C# 14+ vs earlier
- C# 14+: command properties can use the
fieldkeyword (no separate backing field). - Earlier: traditional
_command??= lazy field pattern.