4

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

所有这些都可以在 WPF 中正常工作。我可以将其绑定LocationIdCommandParameter.

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

1 回答 1

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

于 2013-09-01T20:30:52.820 回答