Samantha J T Star
Samantha J T Star

Reputation: 32798

How can I add parameters to a command I am defining in Xamarin?

I have this code that I am using to assign a command:

        

private ICommand aButtonClickedCommand;

public ICommand AButtonClickedCommand => 
       aButtonClickedCommand ??
       (aButtonClickedCommand = new Command(ProcessButtonClickedCommand));

private void ProcessButtonClickedCommand()
{
   App.DB.IncrementPoints(Settings.cfs, phrasesFrame.phrase, 1, 2);
   Change.points = true;
   phrasesFrame.CancelTimer2();
 }

As I wanted to add some parameters I changed it to this:

public ICommand AButtonClickedCommand => 
       aButtonClickedCommand ?? 
       (aButtonClickedCommand = new Command(ButtonClickedCommand((int)Settings.aBtn, 1)));

private void ButtonClickedCommand(int pts, int col)
{
    App.DB.IncrementPoints(Settings.cfs, phrasesFrame.phrase, pts, col);
    Change.points = true;
    phrasesFrame.CancelTimer2();
}

However I am getting an error message saying:

Error CS1503: Argument 1: cannot convert from 'void' to 'System.Action' (CS1503)

Upvotes: 1

Views: 57

Answers (1)

Nkosi
Nkosi

Reputation: 247088

You can use a delegate for the command

public ICommand AButtonClickedCommand => 
    aButtonClickedCommand ?? 
    (aButtonClickedCommand = new Command(() => ButtonClickedCommand((int)Settings.aBtn, 1)));

Reference Command Class

Upvotes: 1

Related Questions