我有一个包含 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/