Passing a parameter using MvvmCross MvxBind to ItemClick using MvxGridView

I have a ViewModel that includes an ObservableCollection LocationViewModel . They appear as tiles in the grid. Each LocationViewModel stores a LocationId , which is passed as a parameter when you click on a grid element. MvxCommand, called when an item is clicked, looks something like this:

 // Constructor public MainViewModel() { LocationSelectedCommand = new MvxCommand<string>(OnLocationSelected); } // MvxCommand calls this private void OnLocationSelected(string locationId) { // Open a new window using 'locationId' parameter } 

All this works correctly with WPF. I can bind LocationId as CommandParameter .

 <view:LocationTileView Content="{Binding }" Command="{Binding ElementName=groupView, Path=DataContext.LocationSelectedCommand}" CommandParameter="{Binding LocationId}" /> 

Is there any equivalent syntax in Android for passing a parameter? This does not work, but I'm looking for something like what is in the MvxBind line:

 <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
source share
1 answer

You can use MvxCommand<T> with ItemClick

This will return an element to you:

 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 eg item.LocationId } 

If this helps, there are several examples of this at https://github.com/slodge/MvvmCross-Tutorials/ - for example. inside the Daily Dilbert ListViewModel.cs

+10
source

Source: https://habr.com/ru/post/1499841/


All Articles