2013-08-30 32 views
4

我有一個包含LocationViewModel的ObservableCollection的ViewModel。這些在網格中顯示爲瓦片。每個LocationViewModel存儲一個LocationId,當單擊網格中的一個圖塊時,它將作爲參數傳遞。單擊某個項目時調用的MvxCommand看起來是這樣的:使用MvxGridView在ItemClick上傳遞參數MvvmCross MvxBind

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

回答

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/ - 例如裏面每日Dilbert ListViewModel.cs