Skip to content

[RelayCommand] does not work with default/optional arguments as the command parameter #1088

@bzd3y

Description

@bzd3y

Describe the bug

The following command compiles, but causes a runtime error when called.

public override async Task SomeCommandAsync(bool optional = false, CancellationToken cancellationToken = default)
{

}

The same command without the first optional argument does work, i.e. the optional CancellationToken is handled correctly, as far as I can tell.

If it isn't possible to dispatch the command with that default/optional argument, then the analyzer should probably produce a compiler error notifying the user that RelayCommands cannot have optional arguments.

Regression

No response

Steps to reproduce

  1. Paste the code above into a class inside a project that references the CommunityToolkit
  2. Run the application

Expected behavior

Either pass in the provided value or the default or generate a compiler error to notify the user that commands cannot have optional arguments.

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

CommunityToolkit.Mvvm 8.4.0

Additional context

No response

Help us help you

No, just wanted to report this

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