Skip to content

[XSG][BindingSourceGen] Add support for CommunityToolkit.Mvvm RelayCommandAttribute source generator to compiled bindings #32953

@simonrozsival

Description

@simonrozsival

The CommuityToolkit.Mvvm package has a source generator for generating relay commands like this:

[RelayCommand]
private async Task Save()
{
   // ...
}

will produce:

/// <summary>The backing field for <see cref="SaveCommand"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.3.0.0")]
private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand? saveCommand;
/// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand"/> instance wrapping <see cref="Save"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.3.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand SaveCommand => saveCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand(new global::System.Func<global::System.Threading.Tasks.Task>(Save));

It is common to then bind this command to a button Click or any other event:

<Button Text="Save" Command="{Binding SaveCommand}" />

Note: the attribute does not have any option to override the generated property name. The name is always $"{propertyName}Command".

Currently, the binding compiler will fail to compile, because the SaveCommand property is generated by another source generator which it cannot see. This is a limitation of both the BindingSourceGen and XAML SourceGen. The fix should be in shared code.

Metadata

Metadata

Assignees

Labels

area-xamlXAML, CSS, Triggers, BehaviorsxsgXaml sourceGen

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions