Xamarin формирует передачу параметров между ViewModels

#c# #xamarin.forms

#c# #xamarin.forms

Вопрос:

У меня есть две viewmodels. Первый SxCaseDetailViewModel содержит сведения для одного SxCaseId. Вторая viewmodel CaseStaffJoinViewModel содержит весь персонал, связанный с SxCaseId. Я не получаю никаких связанных данных в моем CaseStaffJoinViewModel. Как мне передать int SxCaseId в CaseStaffJoinViewModel, чтобы он отображал все связанные данные?

ViewModel SxCaseDetailViewModel

открытый класс 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
        {
            
        }
    }

}
 

Служба данных

 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;
    }
 

Ответ №1:

вы передаете SxCase в своей навигации

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

но вы проверяете только int при получении данных в InitializeAsync

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

типы должны совпадать — вам нужно либо передать int , либо проверить наличие SxCase