Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't pass a single parameter to lambda function in MVVM Light Toolkit's RelayCommand

I don't know if there's a difference between Josh Smith's and Laurent Bugnion's implementations of RelayCommand or not, but everywhere I've looked, it sounds like the Execute portion of RelayCommand can take 0 or 1 parameters. I've only been able to get it to work with 0. When I try something like:

public class Test
{
    public RelayCommand MyCommand { get; set; }

    public Test()
    {
        MyCommand = new RelayCommand((param) => SomeFunc(param));
    }

    private void SomeFunc( object param)
    {
    }
}

I get the error: Delegate 'System.Action' does not take '1' arguments. Just to make sure I am not insane, I went to the definition of RelayCommand to make sure I didn't have some rogue implementation in my solution somewhere, but sure enough, it was just Action, and not Action<>.

What on earth am I missing here?

like image 352
Dave Avatar asked Feb 27 '23 21:02

Dave


1 Answers

The non-generic implementation of RelayCommand (in MVVM Light) does not accept a parameter. Use RelayCommand<Object> instead, or (even better) RelayCommand<YourCustomType> so the parameter to SomeFunc is strongly typed.

like image 172
Matt Hamilton Avatar answered Apr 07 '23 08:04

Matt Hamilton