2
votes

How can I pass more than 1 parameter to a MvxCommand? For example:

private MvxCommand<int> _addNumbers;
public ICommand AddNumbers
{
    get
    {
        _addNumbers = _addNumbers ?? new MvxCommand<int>(this.OnAddNumbers);
        return _addNumbers;
    }
}

This command takes only 1 int. I need it to take 2.

Also, can a command return a value?

2

2 Answers

3
votes

I think your best option is to use a Tuple to turn your two parameters into a single object.

    MvxCommand<Tuple<int, int>> _addNumbers;
    public IMvxCommand AddNumbers
    {
        get
        {
            return _addNumbers = _addNumbers ?? new MvxCommand<Tuple<int, int>>(OnAddNumbers);
        }
    }

EDIT: As for your second question:

Also, can a command return a value?

An MvxCommand is a type, not a method. It doesn't directly return anything. During the execution of the get on your public MvxCommand property you can call a separate method which returns something, or do so within the Action (OnAddNumbers in your case) that you return.

1
votes

I recommend to use the following flexible solution: create DTO Class to store you parameters, serialize it and pass serialized object - you need command only with string parameter. Below is example:

`
public class DTO
{ 
   int number1;
   int number2;
}
private MvxCommand<string> _addNumbers;
public ICommand AddNumbers
{
    get
    {
        _addNumbers = _addNumbers ?? new MvxCommand<string>(() =>
        {
             _addNumber = JsonConvert.SerializeObject(new DTO() { number1 = 1, number2 = 2});
        });
        return _addNumbers;
    }
}`

And deserialize it like this:

JsonConver.DeserializeObject<DTO>(youSerializedDTO);