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.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

[BUG] RelayCommand with parameter for menuItem / contextAction on windows #766

Closed SamuelJames101 closed 1 year ago

SamuelJames101 commented 1 year ago

Describe the bug

I have created a ListView. Created context actions. If a menu item in the context action points to a [RelayCommand] which takes a parameter then the list view items don't show.

Take away the parameter and it works.

Repo : https://github.com/SamuelJames101/comtoolkitIssueMaui

Steps to reproduce

1. Create a ListView
2. Populate the list
3. Add ContextActions
4. For one of the menu items point to a RelayCommand that requires a parameter.

Expected behavior

A ListView item pointer to a RelayCommand should show with or without a paramater.

Screenshots

Screenshot 2023-10-02 at 10 26 27

IDE and version

VS 2022 Preview

IDE version

17.8.0 Preview 1.0

Nuget packages

Nuget package version(s)

8.2.1

Help us help you

No, just wanted to report this

Sergio0694 commented 1 year ago

There isn't really anything that looks wrong in that viewmodel, and I'm not familiar with the MAUI syntax at all. It's not clear at all that there's any problem with the MVVM Toolkit here, unless proven otherwise. This should be in the MAUI repo 🙂

I will close this, but feel free to share a minimal repro and I can reopen this. Until then, this seems more likely to either be some bug in the XAML code there, or some bug in MAUI. That viewmodel just seems completely fine to me.

SamuelJames101 commented 1 year ago

@Sergio0694 If I bind to a normal Microsoft.maui.controls.command it works. It also isn't just me : https://stackoverflow.com/questions/76681900/binding-contextactions-menuitem-with-communitytoolkit-mvvm-in-maui.

I have been told to raise this issue in the Dotnet side apposed to the maui side which I originally raised it. https://github.com/CommunityToolkit/Maui/issues/1435

Sergio0694 commented 1 year ago

Can you reproduce the same issue if you manually declare the command? Ie. not using [RelayCommand], but rather defining a command property yourself and having it return the appropriate RelayCommand instance. Does that work? 🤔

SamuelJames101 commented 1 year ago

@Sergio0694 Is this what you mean?

public MainPageViewModel()
    {
        this.MyNormalCommand = new Command<string>(this.MyMenuItemMethodParameter);
    }

public Command MyNormalCommand { get; set; }

Then in XAML

Command="{Binding BindingContext.MyNormalCommand, Source={x:Reference Name=test}}" CommandParameter="{Binding .}"

This is how I would do it if I was not using communitytoolkit.mvvm and this works

Sergio0694 commented 1 year ago

Yes, but use the RelayCommand<T> type from the MVVM Toolkit.

SamuelJames101 commented 1 year ago

@Sergio0694 The below results in the same as the first list in the screenshot

    public MainPageViewModel()
    {
        this.MyNormalCommand = new RelayCommand<string>(this.MyMenuItemMethodParameter);
    }

    public RelayCommand<string> MyNormalCommand { get; set; }

Removing < string > gives the same result as the second list in the screenshot

    public MainPageViewModel()
    {
        this.MyNormalCommand = new RelayCommand(this.MyMenuItemMethod);
    }

    public RelayCommand MyNormalCommand { get; set; }