android - 使用 MvxGridView 在 ItemClick 上通过 MvvmCross MvxBind 传递参数

标签 android android-layout mvvmcross

我有一个包含 LocationViewModel 的 ObservableCollection 的 ViewModel。这些在网格中显示为图 block 。每个 LocationViewModel 都存储一个 LocationId,当单击网格中的图 block 时,它会作为参数传递。单击项目时调用的 MvxCommand 如下所示:

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

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

所有这些都适用于 WPF。我可以将 LocationId 绑定(bind)为 CommandParameter

<view:LocationTileView 
    Content="{Binding }" 
    Command="{Binding ElementName=groupView, Path=DataContext.LocationSelectedCommand}" 
    CommandParameter="{Binding LocationId}" 
/>

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

最佳答案

您可以使用 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
}

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

关于android - 使用 MvxGridView 在 ItemClick 上通过 MvvmCross MvxBind 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537957/

相关文章:

安卓 Espresso : checking for a checked checkbox

c# - 在 Xamarin.Android 和 MvvmCross 中定义应用程序类的基类

java - 任务 ':app:mergeExtDexDebug' 执行失败

android - 如何挂载Android Phone 的sdcard 在PC 和Phone 上都可见?

java - 交错网格布局管理器 : Adding Margin makes the layout items pushed to the side

Android:自定义 ListView 中的单选按钮

c# - MvvmCross : conventional plugin bypass

android - MvxNotifyPropertyChanged 应用程序在 SetProperty 上崩溃

android - Google 应用内购买始终会获得退款

Android 2.1标准回收站图标