[Bug] RelayCommand Source Generator Fails Silently for Methods Starting With ‘On'
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
Research direction
Start with the RelayCommand source generator and the name-handling path for methods beginning with On. Reproduce the issue using the provided TestViewModel and binding, then compare the generated property name with the documented behavior. Done means OnPointerPressed generates PointerPressedCommand and the Avalonia binding compiles.
Written by the indexing model from the issue text.
Description
Describe the bug
Methods annotated with [RelayCommand] whose names start with On fail to generate command properties.
The documentation states that:
The name of the generated command will be created based on the method name. The generator will use the method name and append "Command" at the end, and it will strip the "On" prefix, if present. Additionally, for asynchronous methods, the "Async" suffix is also stripped before "Command" is appeneded.
However, the actual behaviour I am experiencing contradicts this claim.
Regression
No response
Steps to reproduce
Prerequisites:
- .NET 10
- C# 13
- Create a new Avalonia (Ver. 12.1.3) desktop project. Your project file should include:
<TargetFramework>net10.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
-
Add the
CommunityToolkit.Mvvm(Ver. 8.4.2) NuGet package. -
Create a ViewModel with
[RelayCommand]methods starting withOn.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Diagnostics;
namespace MyTestApp.Avalonia.ViewModels;
public partial class TestViewModel : ObservableObject
(etc.)
[RelayCommand]
private void OnPointerPressed()
{
Debug.WriteLine("Pointer pressed.");
}
[RelayCommand]
private void AsdfPointerPressed()
{
Debug.WriteLine("Pointer pressed.");
}
- Create a
TestView. Attempt to bind toOnPointerPressedCommand.
<Window
x:Class="MyTestApp.Avalonia.Views.TestView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:MyTestApp.Avalonia.ViewModels"
Name="TestView"
x:DataType="vm:TestViewModel">
<!-- This binding will fail to compile. -->
<Button Command="{Binding OnPointerPressedCommand}" />
<!-- This binding will compile successfully. -->
<Button Command="{Binding AsdfPointerPressedCommand}" />
</Window>
Expected behavior
In the ViewModel, [RelayCommand] private void OnPointerPressed() { } would generate a PointerPressedCommand property, which the View could then execute.
Screenshots
No response
IDE and version
VS 2022
IDE version
18.10.2
Nuget packages
- CommunityToolkit.Common
- CommunityToolkit.Diagnostics
- CommunityToolkit.HighPerformance
- CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)
8.4.2
Additional context
No response
Help us help you
No, just wanted to report this
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from CommunityToolkit/dotnet
-
bug :bug:
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
CommunityToolkit/dotnet#1206 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
Difficulty 1/5 Under an hour Newbie friendliness 68/100
CommunityToolkit/dotnet#648 ·
-
feature request :mailbox_with_mail:
Difficulty 5/5 Over a week Newbie friendliness 45/100
CommunityToolkit/dotnet#1214 ·
-
bug :bug:
Difficulty 4/5 3-5 days Newbie friendliness 58/100
CommunityToolkit/dotnet#1208 ·
All issues in CommunityToolkit/dotnet
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NethermindEth/nethermind#14012 ·
Maintainers usually reply within 1 day
-
dependencies Status: Triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
json-schema-org/website#2518 ·
Maintainers usually reply within 1 day
-
agentic-workflows
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
builtbybel/Flyoobe#498 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
builtbybel/CrapFixer#112 ·