0
votes

Question: Perhaps the challenge could be stated this way: How do I bind a single property in a content page to a globally stored variable in Xamarin Forms?

Details: I am using the MVVM pattern. I have a navigation content page (1 of 3 such pages) with a Picker object which is populated dynamically from the collectionModel and said model is read/write. I am attempting to persist the SelectedItem (or index, whichever is most appropriate) thru all 3 content pages such that navigation from page to page shows the same item (from the user's perspective). How should I do this?

I can set the Picker.SelectedIndex manually in ContentPage_Appearing() event. I would much rather use binding.

1
If you are going to downvote, please explain why in the comments and give me the chance to make the question better. I cannot think of a good reason to downvote this question which has received multiple votes in both directions. - maplemale

1 Answers

1
votes

Follow these steps:

  1. Create a static class, like this:

    public static class DataClass
    {
       public static int PickerSelectedIndex = 0;
    }
    
  2. Add the xmlns:local in ContentPage mark in each Content Page, like this:

    xmlns:local="clr-namespace:DataPersist"
    
  3. Binding the data for your controls in each page's Xaml, like this:

    <Picker  x:Name="picker" SelectedIndex="{x:Static local:DataClass.PickerSelectedIndex}">
    

It works like this:

enter image description here