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 で正しく機能します。LocationIdとしてバインドでき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" />
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 に答える