Search code examples
c#androidxamarinxamarin.androidmvvmcross

Pass ItemClicked in MvxRecyclerView object to new View


I'm newbie in Xamarin.

I have MvxRecyclerView to show list of Cars. Clicking in a car let user to display full specification of chosen car. I have problem with displaying full specification of chosen car in new activity (and at the same time pass object between viewmodels)

My MvxRecyclerView .xml looks like:

    <mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"
        local:MvxItemTemplate="@layout/listitem_car"
        local:MvxBind="ItemsSource Cars; ItemClick NavigateCommand"
        />

And my CarsViewModel with "empty" navigation to CarItemViewModel:

public class CarsViewModel : MvxViewModel
    {
        public CarsViewModel(IMvxNavigationService navigationService)
        {
            Cars = new MvxObservableCollection<Car>();
            _navigationService = navigationService;
            NavigateCommand = new MvxAsyncCommand(() => _navigationService.Navigate<CarItemViewModel>());
        }

        private MvxObservableCollection<Car> _cars;
        public MvxObservableCollection<Car> Cars
        {
            get => _cars;
            set
            {
                _cars = value;
                RaisePropertyChanged(() => Cars);
            }
        }

        public override async Task Initialize()
        {
            await base.Initialize();
            CarService carService = new CarService();
            await Task.Run(async () =>
             {
                 Cars = await carService.GetCars();
             });
        }

        private readonly IMvxNavigationService _navigationService;
        public IMvxAsyncCommand NavigateCommand { get; private set; }

Do you know how is it possible to move data of chosen mvxrecyclerview item to new view using MVVMCross? Unfortunately, I don't understand what MVVMCross' documentation says about this (it looks so poor in my opinion).

I would appreciate for any help.

EDIT 1: I've changed a bit description of my problem for more transparent.


Solution

  • I think the document is clear about passing parameter, here:

    When you navigation, you should pass a MyObject of CarItemViewModel:

       await _navigationService.Navigate<CarItemViewModel, MyObject>(new MyObject());
    

    And then you can get the MyObject in the CarItemViewModel and use it:

    public class CarItemViewModel: MvxViewModel<MyObject>
    {
        private MyObject _myObject;
    
        public override void Prepare()
        {
            // first callback. Initialize parameter-agnostic stuff here
        }
    
        public override void Prepare(MyObject parameter)
        {
            // receive and store the parameter here
            _myObject = parameter;
        }
        
        public override async Task Initialize()
        {
            await base.Initialize();
    
            // do the heavy work here
        }
    }