CommunityToolkit / CommunityToolkit/dotnet
wrong Type of Command Parameter leads to `ThrowArgumentExceptionForInvalidCommandArgument`
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Describe the bug
I have a MAUI app on Windows which shows a list of Buttons with a Click Command Binding to the ViewModel via
```xml
...
```
The Command in the MainViewModel looks like the following
```csharp
[RelayCommand]
private void Click(MyItem obj)
{
//do something with obj
}
```
Once i click on one of the buttons the application crashes with the following exception:
```
Parameter "parameter" (object) cannot be of type MauiApp1.MainViewModel, as the command type requires an argument of type MauiApp1.MyItem. (Parameter 'parameter')
at CommunityToolkit.Mvvm.Input.RelayCommand`1.ThrowArgumentExceptionForInvalidCommandArgument(Object parameter)
at CommunityToolkit.Mvvm.Input.RelayCommand`1.Execute(Object parameter)
at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
at Microsoft.Maui.Controls.Button.SendClicked()
at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked()
at Microsoft.Maui.Handlers.ButtonHandler.OnClick(Object sender, RoutedEventArgs e)
at WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.b__1_0(Object sender, RoutedEventArgs e)
at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)
--- End of stack trace from previous location ---
at WinRT.ExceptionHelpers.g__Throw|20_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IControlOverridesMethods.OnPointerReleased(IObjectReference _obj, PointerRoutedEventArgs e)
at Microsoft.UI.Xaml.Controls.Control.OnPointerReleased(PointerRoutedEventArgs e)
at Microsoft.UI.Xaml.Controls.Control.Microsoft.UI.Xaml.Controls.IControlOverrides.OnPointerReleased(PointerRoutedEventArgs e)
at ABI.Microsoft.UI.Xaml.Controls.IControlOverrides.Do_Abi_OnPointerReleased_3(IntPtr thisPtr, IntPtr e)
```
Observing the error message it seems that the `Type` of the `CommandParameter` is wrongly set to `MainViewModel`
In the repro there is also the code in plain MAUI without the use of CommunityToolkit.Mvvm which works fine.
```csharp
//works fine
public ICommand MyClickCommand { get; set; }
public MainViewModel()
{
MyClickCommand = new Command(DoClick);
}
private void DoClick(MyItem item)
{
ClickedItem = item.Name;
}
```
### Regression
can't say for sure but I think this didn't happen in a .NET 7 project
### Steps to reproduce
```text
1. Create .NET MAUI app with a list of buttons
2. Set CommandParameter Binding to .
3. Setup [RelayCommand] in ViewModel
4. Click button
5. 💥
```
### Expected behavior
No crash and correct passing of the `Binding .` object
### Screenshots
_No response_
### IDE and version
VS 2022, VS 2022 Preview
### IDE version
_No response_
### Nuget packages
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)
### Nuget package version(s)
8.2.1
### Additional context
Repro: [MauiApp1.zip](https://github.com/CommunityToolkit/dotnet/files/12830082/MauiApp1.zip)
Original Issue: https://github.com/dotnet/maui/issues/17707
### Help us help you
No, just wanted to report this
Contributor guide
Assessment
This issue has not been assessed yet.