MvxCommand With CommandParameter 绑定到字段

MvxCommand With CommandParameter binding to field

我正在尝试使用 MvxCommandCommandParameter:

   <Mvx.MvxListView
   style="@style/MyList"
   local:MvxBind="ItemsSource Items;"
   local:MvxItemTemplate="@layout/itemfavorite" />

这是我在ViemModel中的属性:

    public List<Data> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            RaisePropertyChanged(() => Items);
        }
    }

这是我的 model:

public class Data
{
   public int Id{get;set;}
   public string Name {get;set;}
   public ICommand Action {get;set;}
}

这是我的command(在数据中使用):

    public ICommand MyCommand
    {
        get
        {
            return new MvxCommand<int>(async (id) =>
            {
               .....
            }
    }

这是我的MvxItemTemplate: ....

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:layout_margin="10dp"
    local:MvxBind="Click Action,CommandParameter=Id"/>

如果我使用“CommandParameter=Id”- 我得到未处理的异常。 “CommandParameter=1” - 这是工作。但是我需要传递一个 CommandParameter 值字段 Id。可能吗?

CommandParameter 的值是一个 static 值。不幸的是,您不能使用 属性 的名称作为命令参数传递。

您遇到异常,因为 Mvx 试图将 string "Id" 转换为 int.

无需传递 Id,您只需在命令处理程序中使用它即可。

public ICommand MyCommand
{
    get
    {
        return new MvxCommand(async () =>
        {
           // use Id here
        }
    }
}