Search code examples
c#xamarinmvvmxamarin.androidmvvmcross

Pass object from parent to child in Xamarin MvvmCross


I have a SlideListViewModel:

public class SlideListViewModel : MvxViewModel
{
    private readonly IServerClient _serverClient;
    private ICollection<SlideListItemViewModel> _slides;
    private IMapperService _mapper;

    public ICollection<SlideListItemViewModel> Slides { get => _slides; set => SetProperty(ref _slides, value); }

    public SlideListViewModel(IServerClient serverClient, IMapperService mapper)
    {
        _serverClient = serverClient;
        _mapper = mapper;
    }

    public override async Task Initialize()
    {
        await base.Initialize();
        Slides = _mapper.Map<ICollection<SlideListItemViewModel>>(await _serverClient.GetSlideList());
    }
}

And I have a SlideListItemViewModel:

public class SlideListItemViewModel : MvxViewModel
{
    private long _id;
    private string _title;

    public long Id { get => _id; set => SetProperty(ref _id, value); }

    public string Title { get => _title; set => SetProperty(ref _title, value); }

    public IMvxCommand DeleteCommand => new MvxCommand<IServerClient>(DeleteCommandHandler);

    public async void DeleteCommandHandler(IServerClient serverClient)
    {
        var delete = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
        {
            Title = "Delete slide",
            Message = "Are you sure you want to delete this slide?",
            OkText = "Yes",
            CancelText = "No"
        });

        if (!delete)
            return;

        //Here I should user my serverClient
    }
}

Is it possible to pass command an interface _serverClient from SlideListViewModel? I tried binding in slidelistitemviewmodel.axml, but nothing happened...

 <Button
    local:MvxBind="Click DeleteCommand, CommandParameter=_serverClient"
    style="@style/ButtonSlide"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Delete" />

Could someone please help me with that???


Solution

  • You need to have it available in SlideListItemViewModel. SlideListItemViewModel.xaml doesn't have access to SlideListViewModel. Don't bother making it a command parameter, just pass it in from the SlideListViewModel when you create the items. Add the property in SlideListItemViewModel.cs:

    public class SlideListItemViewModel : MvxViewModel
    {
        private long _id;
        private string _title;
        private IServerClient _serverClient;
    
        public long Id { get => _id; set => SetProperty(ref _id, value); }
    
        public string Title { get => _title; set => SetProperty(ref _title, value); }
    
        public IServerClient ServerClient {get => _serverClient; set => SetProperty(ref _serverClient, value; }
    
        public IMvxCommand DeleteCommand => new MvxCommand(DeleteCommandHandler);
    
        public async void DeleteCommandHandler()
        {
            var delete = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
            {
                Title = "Delete slide",
                Message = "Are you sure you want to delete this slide?",
                OkText = "Yes",
                CancelText = "No"
            });
    
            if (!delete)
                return;
    
            //Here I should user my serverClient
        }
    }
    

    Pass it in on creation in SlideListViewModel.cs:

    public override async Task Initialize()
    {
        await base.Initialize();
        Slides = _mapper.Map<ICollection<SlideListItemViewModel>>(await _serverClient.GetSlideList());
        foreach(var slide in Slides)
        {
            slide.ServerClient = _serverClient;
        }
    }
    

    and take it out of the binding in SlideListItemViewModel.xaml:

    local:MvxBind="Click DeleteCommand"