简体   繁体   中英

Xamarin Forms Passing Parameters Between ViewModels

I have two viewmodels. The first SxCaseDetailViewModel contains the details for one SxCaseId. The second viewmodel CaseStaffJoinViewModel containes all of the staff related to the SxCaseId. I am not getting any related data in my CaseStaffJoinViewModel. How do I pass the int SxCaseId to the CaseStaffJoinViewModel so that it will show all related data?

ViewModel SxCaseDetailViewModel

public class SxCaseDetailViewModel: ViewModelBase {

    private ISxCaseDataService _sxCaseDataService;
    private ICaseStaffJoinDataService _caseStaffJoinDataService;
    private SxCase _selectedSxCase;


    public SxCaseDetailViewModel(IConnectionService connectionService,
        INavigationService navigationService, IDialogService dialogService,
        ICaseStaffJoinDataService caseStaffJoinDataService,
        ISxCaseDataService sxCaseDataService)
        : base(connectionService, navigationService, dialogService)
    {
        _sxCaseDataService = sxCaseDataService;
        _caseStaffJoinDataService = caseStaffJoinDataService;

    }

    public SxCase SelectedSxCase
    {
        get => _selectedSxCase;
        set
        {
            _selectedSxCase = value;
            OnPropertyChanged();
        }
    }


    public override async Task InitializeAsync(object navigationData)
    {
        IsBusy = true;

        SelectedSxCase = (SxCase) navigationData;

        IsBusy = false;
    }

    public ICommand CaseStaffJoinTappedCommand => new Command<SxCase>(OnCaseStaffJoinTapped);


    private void OnCaseStaffJoinTapped(SxCase selectedSxCase)
    {
        _navigationService.NavigateToAsync<CaseStaffJoinViewModel>(selectedSxCase);

    }

}

ViewModel CaseStaffJoinViewModel

public class CaseStaffJoinViewModel : ViewModelBase
{
    private ICaseStaffJoinDataService _caseStaffJoinDataService;
    private ObservableCollection<CaseStaffJoin> _caseStaffJoins;

    public CaseStaffJoinViewModel(IConnectionService connectionService,
        INavigationService navigationService, IDialogService dialogService,
        ICaseStaffJoinDataService caseStaffJoinDataService)
        : base(connectionService, navigationService, dialogService)
    {
        _caseStaffJoinDataService = caseStaffJoinDataService;
    }

    public ObservableCollection<CaseStaffJoin> CaseStaffJoins
    {
        get => _caseStaffJoins;
        set
        {
            _caseStaffJoins = value;
            OnPropertyChanged();
        }
    }


    public override async Task InitializeAsync(object navigationData)
    {
        if (navigationData is int)
        {
            IsBusy = true;
            // Get caseStaffJoins by id
            
            CaseStaffJoins = (ObservableCollection<CaseStaffJoin>) 
                await _caseStaffJoinDataService.GetCaseStaffJoinsAsync((int) navigationData);
            IsBusy = false;
        }

        else
        {
            
        }
    }

}

Data Service

class CaseStaffJoinDataService :BaseService, ICaseStaffJoinDataService
{
    HttpClient _client;

    private readonly IGenericRepository _genericRepository;


    public CaseStaffJoinDataService(IGenericRepository genericRepository,
        IBlobCache cache = null) : base(cache)
    {
        _genericRepository = genericRepository;
        _client = new HttpClient();

    }

    public async Task<ObservableCollection<CaseStaffJoin>> FilterAsync(int sxCaseId)
    {
        UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
        {
            Path = $"{ApiConstants.CaseStaffsEndpoint}/{sxCaseId}"
        };

        IEnumerable<CaseStaffJoin> caseStaffJoins =
            await _genericRepository.GetAsync<IEnumerable<CaseStaffJoin>>(builder.ToString());

        
        return caseStaffJoins?.ToObservableCollection();
        
    }

    public async Task<CaseStaffJoin> GetCaseStaffJoinBySxCaseIdAsync(int sxCaseId)
    {
        UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
        {
            Path = $"{ApiConstants.CaseStaffsEndpoint}/{sxCaseId}"
        };

        var caseStaffJoin = await _genericRepository.GetAsync<CaseStaffJoin>(builder.ToString());

        return caseStaffJoin;
    }

    public async Task<IEnumerable<CaseStaffJoin>> GetCaseStaffJoinsAsync(int sxCaseId)
    {
        UriBuilder builder = new UriBuilder(ApiConstants.BaseApiUrl)
        {
            Path = $"{ApiConstants.CaseStaffsEndpoint}/{sxCaseId}"
        };

        var caseStaffJoins = await _genericRepository.GetAsync<List<CaseStaffJoin>>(builder.ToString());



        return caseStaffJoins;
    }

you are passing an SxCase in your navigation

private void OnCaseStaffJoinTapped(SxCase selectedSxCase)
{
    _navigationService.NavigateToAsync<CaseStaffJoinViewModel>(selectedSxCase);
}

but you are only checking for an int when receiving the data in InitializeAsync

public override async Task InitializeAsync(object navigationData)
{
    if (navigationData is int)

the types need to match - you either need to pass an int or check for an SxCase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM