Wednesday, June 12, 2013

CommandParameter binding


We recently added a special CommandParameter parse step to allow the binding to specify fixed values (strings, ints, bools) to ICommand bindings.

This can be used as:

        local:MvxBind="Click MyCommand,CommandParameter=Thursday"

which uses a ValueConverter to turn a Click into a call on:

       MyCommand.Execute("Thursday")

So can be received in

       new MvxCommand(day => DoAction(day));
      
This helps where you want to reuse a command across multiple buttons

However, please note that this cannot be used currently with valueconverters - as it itself uses a valueconverter to achieve the effect.

Hope that helps :)

5 comments:

  1. Hi, Slodge!
    new MvxCommand(day => DoAction(day));

    Did you mean:
    new MvxCommand<string>(day => DoAction(day));

    But in this case day equals null :(
    What I'm doing wrong?

    <Button
    android:text="myButton"
    android:id="@+id/changeToAppetite"
    local:MvxBind="Click MyCommand, CommandParameter=sometext" />

    MyCommand= new MvxCommand<string>(param =>
    {
    if (param == "sometext") // there param == null
    {
    ...
    }
    }

    ReplyDelete
    Replies
    1. Sadly the missing <> are part of blogger :/ Will move to a markdown based blog one day.

      The code you've posted works here - maybe post a fuller example on StackOverflow or a repo on Github. The code was committed 17 days ago so it should be in the latest release packages https://github.com/slodge/MvvmCross/commit/a71b7c08f7af899d59a20e140a6fe5cff36c3d24

      Delete
  2. When will this be released through Nuget? It still doesn't appear to be in the latest packages.

    ReplyDelete
    Replies
    1. This should be in the beta packages (visible if pre-release is chosen). Further i believe it is in the stable release *except* it requires the special command parameter value converter to be manually registered.

      Delete