CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.

Home Page:https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CommandParameters other than Strings are not acceptable for RelayCommands?

MorElmaliach opened this issue · comments

Describe the bug

Hello,

I'm trying to upgrade our code base from using MVVMLight to the CommunityToolkit, so far a part from specific small things it was smooth, but I've encountered a big issue during execution. Some of our buttons are bound to commands with a command parameter. These parameters are not always strings, some of them are bool ("False", "True"), some of them are int ("-1", "1"). For any commandparameter other than an explicit string with a fitting command, there's an exception thrown during runtime.

Code example from a test project I've made to isolate the issue:
<!--bar--> <StackPanel Grid.RowSpan="3" Grid.Column="0"> <Button Content="Increase" Width="70" Margin="15" Command="{Binding TestCommand}" CommandParameter="1"/> <Button Content="Decrease" Width="70" Margin="15" Command="{Binding TestCommand}" CommandParameter="-1"/> </StackPanel>

My viewmodel:
public ICommand TestCommand => new RelayCommand<int>((idx) => { _leftFocusRatio += idx; });
During runtime, once LoadComponent is executed and this part of the XAML is parsed, the exception is thrown:

System.ArgumentException: 'Parameter "parameter" (object) cannot be of type System.String, as the command type requires an argument of type System.Int32.
Parameter name: parameter'

I could not find any specific evidence in the existing or past issues, nor in the documentation for any limitations on the CommandParameters usable this way. Is this a bug or is this an issue?

Considered workarounds right now:

  • Changing all places we use a number or bool to a relaycommand with a string and then parse them. Problem with this approach is parameters we use in Interactivity EventToCommand actions which lead to the same crashes (it's not able to parse strings to event args).
  • Making commands for every interaction (increase command, decrease command, instead of one setcommand with a value parameter). Would double up our existing code and lead to bloat.

Regression

No response

Steps to reproduce

You can simply run the project attached. It's running on visual studio 17.6.5.
Otherwise, create a button with a command attached to it in the ViewModel.
Make the command a relaycommand with a template parameter of an int.
In the XAML add the attribute "CommandParameter" with a number, such as 1. 
Execute the project.

Expected behavior

I expect to be able to use binding to int or bool (or event args) parameters just as I did with MVVMLight before.
packages.zip

Screenshots

No response

IDE and version

VS 2022

IDE version

17.6.5

Nuget packages

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

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

No, just wanted to report this