WHY I'M GETTING THE ERROR: cannot convert from 'method group' to 'Action' ????

SIUSSIUS PSMember
edited April 2016 in Xamarin.Forms

I'm trying to get a TextCell click inside my ViewModel via Binding.
I used this to do it.

`
public ICommand ItemCommand
{
get;
set;
}

public MainViewModel ()
{
this.ItemCommand = new Command (this.ItemAction);
}

private void ItemAction(object sender, EventArgs e)
{
var menuItem = sender as MenuItem;
var item = menuItem.CommandParameter as Trip;
this._navigationService.NavigateTo(new Views.DetailView(item));
}

`

WHY I'M GETTING THE ERROR: cannot convert from 'method group' to 'Action' ????

Answers

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @SIUS - change this line of code

    this.ItemCommand = new Command (() => { this.ItemAction(); } );

  • SIUSSIUS PSMember

    @AdamP
    man, I changed the code... but now I'm getting this error below

    there's no argument given that corresponds to the required formal parameter 'sender' of the method

    private void ItemAction(object sender, EventArgs e)
    {
    var menuItem = sender as TextCell;
    var item = menuItem.CommandParameter as Trip;
    this._navigationService.NavigateTo(new Views.DetailView(item));
    }

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @SIUS - that is because the ItemAction method requires parameters.

    In order to get the CommandParameter you will have to pass it through with the command. Something like this.

    this.ItemCommand = new Command ((parameter) => { this.ItemAction(parameter); } );
    
    private void ItemAction(object parameter)
    {
         var item = parameter as Trip;
         this._navigationService.NavigateTo(new Views.DetailView(item));
    }
    
Sign In or Register to comment.