Skip to content

[Bug] RelayCommand Source Generator Fails Silently for Methods Starting With ‘On' #1215

Description

@JosiahDanger

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
  1. Create a new Avalonia (Ver. 12.1.3) desktop project. Your project file should include:
<TargetFramework>net10.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
  1. Add the CommunityToolkit.Mvvm (Ver. 8.4.2) NuGet package.

  2. Create a ViewModel with [RelayCommand] methods starting with On.

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.");
	}
  1. Create a TestView. Attempt to bind to OnPointerPressedCommand.
<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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛An unexpected issue that highlights incorrect behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions