使用MvxGridView在ItemClick上使用MvvmCross MvxBind传递参数

Jos*_*osh 4 android android-layout mvvmcross

我有一个包含ObservableCollection的ViewModel LocationViewModel.这些在网格中显示为切片.每个LocationViewModel存储一个LocationId在单击网格中的图块时作为参数传递的图像.单击某个项时调用的MvxCommand看起来像这样:

// Constructor
public MainViewModel()
{
    LocationSelectedCommand = new MvxCommand<string>(OnLocationSelected);
}

// MvxCommand calls this
private void OnLocationSelected(string locationId)
{
    // Open a new window using 'locationId' parameter
}
Run Code Online (Sandbox Code Playgroud)

所有这些都适用于WPF.我可以绑定LocationId一个CommandParameter.

<view:LocationTileView 
    Content="{Binding }" 
    Command="{Binding ElementName=groupView, Path=DataContext.LocationSelectedCommand}" 
    CommandParameter="{Binding LocationId}" 
/>
Run Code Online (Sandbox Code Playgroud)

在Android中是否存在用于传递参数的等效语法?这不起作用,但我正在寻找像MvxBind行中的内容:

<Mvx.MvxGridView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:numColumns="5"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    local:MvxBind="ItemClick LocationSelectedCommand=LocationId; ItemsSource LocationViewModels"
    local:MvxItemTemplate="@layout/locationtileview" />
Run Code Online (Sandbox Code Playgroud)

Stu*_*art 10

您可以使用MvxCommand<T>ItemClick

这会将物品传回给你:

private Cirrious.MvvmCross.ViewModels.MvxCommand<StacksTableItem> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
    get
    {
        _itemSelectedCommand = _itemSelectedCommand ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<MyItem>(DoSelectItem);
        return _itemSelectedCommand;
    }
}

private void DoSelectItem(MyItem item)
{
    // do whatever you want with e.g. item.LocationId
}
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,可以在https://github.com/slodge/MvvmCross-Tutorials/中找到一些这样的例子- 例如在Daily Dilbert ListViewModel.cs中