Mvvm 使用带有CommandParameter绑定的MvxCommand

Mvvm 使用带有CommandParameter绑定的MvxCommand,mvvm,binding,command,xamarin.android,mvvmcross,Mvvm,Binding,Command,Xamarin.android,Mvvmcross,我尝试使用fire,但面临以下问题: MyView.axml包含: <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:la

我尝试使用fire,但面临以下问题: MyView.axml包含:

<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
          }
      });
    }
}
公共类MyViewModel:MvxViewModel
{
public ICommand MyCommand{get;private set;}
公共MyViewModel()
{//param为空
MyCommand=新的MvxCommand(参数=>
{
如果(参数==“foo”)
{
//做点什么
}
否则,如果(参数=“bar”)
{
//做点别的
}
});
}
}
但是当我检查param时,变量是null


我做错了什么?

您的代码在源代码树的顶端为我工作

但这项功能只有两周的历史

我的猜测是,这个特性要么没有进入到您正在使用的版本中,要么它有一个bug

可以检查此绑定的调试跟踪吗?有什么信息吗

  • 如果跟踪表明
    CommandParameter
    是一个未知符号,那么我的猜测是您需要自己构建最新的源代码,或者等待新版本
  • 如果跟踪提示其他问题,则您可以在安装过程中修补该问题

我知道我们解决了一个值转换器问题,基于
cirrial.MvvmCross.Binding.dll的
ValueConverter
不仅仅是覆盖
Setup.ValueConverterAssemblies
来注册我所做的
CommandParameter
所需的
ValueConverter
现在就开始编写CommandParameter代码,您需要做几个修复。axml代码应包含CommandParameter='yourParameter',如下所示:

<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'" />

即使您想要捕捉整数,您仍然需要将其以单引号形式传递,如下所示:CommandParameter='1234'

在C代码中,最重要的是从构造函数中删除MvxCommand。这应被视为财产

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
                }
            });
        }
    }
}
公共类MyViewModel:MvxViewModel
{
公共MyViewModel(){}
公共MvxCommand MyCommand
{
得到
{
返回新的MvxCommand(参数=>
{
如果(参数==“foo”)
{
//做点什么
}
否则,如果(参数=“bar”)
{
//做点别的
}
});
}
}
}

这是在MVVM6中完成的。与以前的版本配合应该很好。

你说得对,斯图尔特!需要监视日志:MvxBind:Warning:132,54找不到命名转换器命令参数I/MvxBind(18314):132,53找不到命名转换器命令参数I/mono stdout(18314):MvxBind:Warning:132,53找不到命名转换器命令参数何时期待最新版本?@Stuart我正在尝试相同的
local:MvxBind=“单击RemoveClick,CommandParameter=。”
我在执行commmand时得到了相同的“。”有什么想法吗?对不起,听起来你和这个2013年的问题完全不一样。在一个新问题中完整地解释你的问题可能是最好的吗?@Stuart,如果我们想使用对象作为参数而不仅仅是字符串?