Using MvxCommand With CommandParameter binding
Asked Answered
H

2

12

I'm trying using fire MvxCommand with CommandParameter, but faced with following problem: MyView.axml contains:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        local:MvxBind="Click MyCommand, CommandParameter=foo" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        local:MvxBind="Click MyCommand, CommandParameter=bar" />
</LinearLayout>

MyViewModel.cs:

public class MyViewModel : MvxViewModel
{
    public ICommand MyCommand { get; private set; }

    public MyViewModel()
    {                                    // param is null
      MyCommand = new MvxCommand<string>(param =>
      {
          if (param == "foo")               
          {
            // do something
          }
          else if (param == "bar")
          {
            // do something else
          }
      });
    }
}

But when I check param variable is null.

What I'm doing wrong?

Hooligan answered 21/6, 2013 at 11:49 Comment(0)
K
10

Your code is working for me on the Head of my source tree.

But this functionality is only two weeks old.

My guess is that this feature either didn't make it into the release that you are working with or there was a bug with it.

Can you check your debug trace for this binding? Is there any information there?

  • If the trace suggests that CommandParameter is an unknown symbol then my guess is that you will need to either build the latest source yourself - or to wait for a new release.
  • If the trace suggests something else then you may be able to patch the problem during setup.

One thing I know we did fix was a value converter issue where the Cirrious.MvvmCross.Binding.dll based ValueConverter's weren't being just by overriding Setup.ValueConverterAssemblies to register the ValueConverter required for this CommandParameter

Kalikow answered 21/6, 2013 at 13:16 Comment(4)
You're right, Stuart! Log's watching needed: MvxBind:Warning:132,54 Could not find named converter CommandParameter I/MvxBind (18314): 132,53 Could not find named converter CommandParameter I/mono-stdout(18314): MvxBind:Warning:132,53 Could not find named converter CommandParameter When to expect recent release?Hooligan
@Kalikow I'm trying the same local:MvxBind="Click OnRemoveClick, CommandParameter=." I'm getting the same "." while executing commmand. any idea please?Zaffer
sorry - doesn't sound like you are getting "the same" as this 2013 question at all. might be best explaining your problem in full in a new question?Kalikow
@Kalikow And if we want to use an object as a parameter and not just a string ?Bury
U
2

I was doing CommandParameter coding today and you need to do couple of fixes. The axml code should contain CommandParameter='yourParameter' it looks like this:

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button1"
    local:MvxBind="Click MyCommand, CommandParameter='foo'" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2"
    local:MvxBind="Click MyCommand, CommandParameter='bar'" />

Even if you want to catch an Integer you still need to pass this in single quotation marks as this: CommandParameter='1234'

In C# code the most important thing is remove MvxCommand from constructor. This should be treated as Property.

public class MyViewModel : MvxViewModel
{
    public MyViewModel() { }

    public MvxCommand<string> MyCommand
    {
        get
        {
            return new MvxCommand<string>(param => 
            {
                if (param == "foo")
                {
                    // do something
                }
                else if (param == "bar")
                {
                    // do something else
                }
            });
        }
    }
}

This was done in MvvmCross6. It should work fine with previous versions.

Unread answered 30/12, 2018 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.