Skip to content

Fix RelayCommand binding inference for On*Async patterns#34183

Merged
StephaneDelcroix merged 2 commits intomainfrom
dev/stdelc/work2
Feb 25, 2026
Merged

Fix RelayCommand binding inference for On*Async patterns#34183
StephaneDelcroix merged 2 commits intomainfrom
dev/stdelc/work2

Conversation

@StephaneDelcroix
Copy link
Copy Markdown
Contributor

@StephaneDelcroix StephaneDelcroix commented Feb 23, 2026

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Summary

  • fix RelayCommand inference for generated command properties when source methods use On*, *Async, or On*Async naming
  • prevent MAUIG2045 false positives for compiled bindings that target those generated command properties
  • add regression coverage in BindingSourceGen and SourceGen unit tests

Related issues

Validation

  • dotnet test src/Controls/tests/BindingSourceGen.UnitTests/Controls.BindingSourceGen.UnitTests.csproj --filter "RelayCommandTests.DetectsRelayCommandMethodWithOnPrefixAndAsyncSuffix"
  • dotnet test src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj --filter "BindingDiagnosticsTests.BindingToRelayCommandGeneratedFromOnAsyncMethod_DoesNotReportPropertyNotFound"

Copilot AI review requested due to automatic review settings February 23, 2026 09:45
@StephaneDelcroix StephaneDelcroix added this to the .NET 10 SR4.1 milestone Feb 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression in MAUI 10.0.40 where the XAML source generator incorrectly reports MAUIG2045 warnings for bindings to command properties generated by CommunityToolkit.Mvvm's [RelayCommand] attribute when the source method uses On*, *Async, or On*Async naming patterns.

Changes:

  • Enhanced RelayCommand method name inference to support all CommunityToolkit.Mvvm naming conventions: Save, SaveAsync, OnSave, and OnSaveAsync all generating SaveCommand
  • Added comprehensive test coverage to prevent regressions

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Controls/src/BindingSourceGen/ITypeSymbolExtensions.cs Added GetRelayCommandMethodNameCandidates helper method to generate all possible method name variations that could produce a given command property name
src/Controls/tests/BindingSourceGen.UnitTests/RelayCommandTests.cs Added test verifying that OnSaveAsync method with [RelayCommand] correctly infers SaveCommand property
src/Controls/tests/SourceGen.UnitTests/BindingDiagnosticsTests.cs Added test verifying that binding to SaveCommand from OnSaveAsync method does not produce MAUIG2045 warning

@PureWeen PureWeen modified the milestones: .NET 10 SR4.1, .NET 10 SR5 Feb 23, 2026
Handle CommunityToolkit [RelayCommand] naming variants (Async suffix and On-prefix) during binding source generation so SaveCommand resolves without MAUIG2045 false positives. Adds regression coverage in BindingSourceGen and SourceGen unit tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CommunityToolkit strips the 'On' prefix during code generation (OnSave() →
SaveCommand, not OnSaveCommand). So when the binding references 'OnSaveCommand',
we should not check for method 'OnSave' since that method generates a different
property. This prevents incorrectly suppressing the MAUIG2045 diagnostic.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines +81 to +84
// Check if the method has the RelayCommand attribute
var hasRelayCommand = method.GetAttributes().Any(attr =>
attr.AttributeClass?.Name == "RelayCommandAttribute" ||
attr.AttributeClass?.ToDisplayString() == "CommunityToolkit.Mvvm.Input.RelayCommandAttribute");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels odd that we have to hardcode names from another library. Would another library appear that has a similar pattern?

Is the real fix, there needs to be some attribute (something?) that RelayCommandAttribute opts into for this behavior? Something more general-purpose?

Copy link
Copy Markdown
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heard the problem is there isn't a way to "order" source generators, so we have a few hacks with known generators.

We can probably find better solution down the road.

@StephaneDelcroix StephaneDelcroix merged commit 46e34d4 into main Feb 25, 2026
28 checks passed
@StephaneDelcroix StephaneDelcroix deleted the dev/stdelc/work2 branch February 25, 2026 20:57
TamilarasanSF4853 pushed a commit to TamilarasanSF4853/maui that referenced this pull request Mar 2, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

## Summary
- fix RelayCommand inference for generated command properties when
source methods use `On*`, `*Async`, or `On*Async` naming
- prevent MAUIG2045 false positives for compiled bindings that target
those generated command properties
- add regression coverage in BindingSourceGen and SourceGen unit tests

## Related issues
- Fixes dotnet#34029
- Duplicate issue dotnet#34086 was closed in favor of dotnet#34029

## Validation
- `dotnet test
src/Controls/tests/BindingSourceGen.UnitTests/Controls.BindingSourceGen.UnitTests.csproj
--filter
"RelayCommandTests.DetectsRelayCommandMethodWithOnPrefixAndAsyncSuffix"`
- `dotnet test
src/Controls/tests/SourceGen.UnitTests/SourceGen.UnitTests.csproj
--filter
"BindingDiagnosticsTests.BindingToRelayCommandGeneratedFromOnAsyncMethod_DoesNotReportPropertyNotFound"`

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SourceGen warning MAUIG2045 with RelayCommand and Async postfix

5 participants