我有一个包含ObservableCollection LocationViewModel
的ViewModel。这些在网格中显示为切片。每个LocationViewModel
存储一个LocationId
,当点击网格中的图块时,该// Constructor
public MainViewModel()
{
LocationSelectedCommand = new MvxCommand<string>(OnLocationSelected);
}
// MvxCommand calls this
private void OnLocationSelected(string locationId)
{
// Open a new window using 'locationId' parameter
}
作为参数传递。单击某个项时调用的MvxCommand看起来像这样:
LocationId
所有这些都适用于WPF。我可以将CommandParameter
绑定为<view:LocationTileView
Content="{Binding }"
Command="{Binding ElementName=groupView, Path=DataContext.LocationSelectedCommand}"
CommandParameter="{Binding LocationId}"
/>
。
<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" />
Android中是否存在用于传递参数的等效语法?这不起作用,但我正在寻找像MvxBind系列中的内容:
{{1}}
答案 0 :(得分: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
}
如果它有帮助,https://github.com/slodge/MvvmCross-Tutorials/中有一些例子 - 例如在Daily Dilbert ListViewModel.cs里面