0
votes

I'm following the following example to create a MasterDetailPage in Xamarin forms, and its working ok.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/master-detail-page

But I've adapted each page to have the ViewModel passed in to each page, so it keeps track of changes etc.

but every time i click between pages, the selected items of pickers seem to reset and throw a NULL exception.

Here is one example of a picker that resets and errors:

XAML

 <Picker x:Name="appointmentDuration" HorizontalOptions="FillAndExpand" Title="Please select appointment length"
                         ItemsSource="{Binding AppointmentDurations, Mode=TwoWay}"   
                        SelectedItem="{Binding AppointmentDurationIndex, Mode=TwoWay}">
</Picker>

ViewModel

  int? _appointmentdurationIndex;
    public int? AppointmentDurationIndex
    {
        get
        {
            return _appointmentdurationIndex;
        }
        set
        {
            if (_appointmentdurationIndex != value)
            {
                _appointmentdurationIndex = value;

                // trigger some action to take such as updating other labels or fields
             var convertedMinutesCount = Double.Parse(value.ToString());
                Contact.AttendanceDetails.EndDateTime = Contact.AttendanceDetails.EndDateTime.AddMinutes(convertedMinutesCount);
                OnPropertyChanged("AppointmentDurationIndex");
            }
        }
    }

Data Type

public ObservableRangeCollection<int?> AppointmentDurations { get; set; }

In the page OnAppearing:

protected async override void OnAppearing()
    {

        base.OnAppearing();
        await viewModel.LoadAppointmentDurationsData(viewModel.Contact.Code);

    }

just after the best way to preserve data between navigation of pages

1

1 Answers

0
votes

You're updating the ItemsSource, Picker reset the SelectedIndex/SelectedItem when ItemsSource is updated.