Retain selected item in picker after re-opening the page

ranju_ranju_ Member ✭✭
edited April 3 in Xamarin.Forms

I am using below code. But selected item is not retaining after re-opening the page

   <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <Picker Title="Select Root Storage" FontFamily="Bold"
                 ItemsSource="{Binding Storages}"
                 ItemDisplayBinding="{Binding value}"
                 SelectedItem="{Binding SelectedStorage, Mode=TwoWay}"
                 />
               <Label x:Name="lblStorage" Text="{Binding Storage }" TextColor="Teal"></Label>
            </StackLayout>

Below is my viewmodel code

public class SettingsViewModel : INotifyPropertyChanged
    {
        private Storage _selectedStorage;

        private string _storage;

        public string Storage
        {

            get { return _storage; }
            set
            {
                if (_storage != value)
                {
                    _storage = value;
                    OnPropertyChanged();
                }
            }
        }

        public List<Storage> Storages { get; set; }

        public Storage SelectedStorage
        {
            get { return _selectedStorage; }
            set
            {
                if (_selectedStorage != value)
                {
                    _selectedStorage = value;

                }


                    Storage = "Root Storage: " + _selectedStorage.value;

            }

        }

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
        public SettingsViewModel()
        {
            Storages = new List<Storage>()
            {
              new Storage(){key=1,value="Internal Storage"},
              new Storage(){key=2,value="External Storage"}
            };


        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Storage
    {
        public int key { get; set; }
        public string value { get; set; }
    }

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    @ranju_ When you reopen this page, the view model is reset too. So the data isn't recorded, the page will show the initial value.
    If you want to persist data at the runtime, you could make your view model as a static one:

    static SettingsViewModel settingsViewModel { set; get; }
    public static SettingsViewModel SharedModel()
    {
        if (settingsViewModel == null)
        {
            settingsViewModel = new SettingsViewModel();
            settingsViewModel.Storages = new List<Storage>()
            {
                new Storage(){key=1,value="Internal Storage"},
                new Storage(){key=2,value="External Storage"}
            };
        }
    
        return settingsViewModel;
    }
    
    // Set the page's binding context
    BindingContext = SettingsViewModel.SharedModel();
    

    If you also want to retrieve it when the app has been forced quitted, you have to store this view model to your device's disk.

  • ranju_ranju_ Member ✭✭

    @Asur said:
    Everything you have in there is part of the class object. How will it retain unless you store in a variable that exists outside the scope of this viewModel i.e if the viewModel is unloaded from memory the data is lost . You need a static variable or singleton to keep data during the app execution

    Suppose I am storing the selected value in to a static variable named "RootFolder" of String Type as below

                  public Storage SelectedStorage
                    {
                        get { return _selectedStorage; }
                        set
                        {
                            if (_selectedStorage != value)
                            {
                                _selectedStorage = value;
    
                            }
    
    
                                Storage = "Root Storage: " + _selectedStorage.value;
                               RootFolder=_selectedStorage.value;
                        }
    
                    }
    

    Now Can you please tell me how can I assign the value stored in RotFolder variable?

  • ranju_ranju_ Member ✭✭

    @ranju_ said:

    @Asur said:
    Everything you have in there is part of the class object. How will it retain unless you store in a variable that exists outside the scope of this viewModel i.e if the viewModel is unloaded from memory the data is lost . You need a static variable or singleton to keep data during the app execution

    Suppose I am storing the selected value in to a static variable named "RootFolder" of String Type as below

                  public Storage SelectedStorage
                    {
                        get { return _selectedStorage; }
                        set
                        {
                            if (_selectedStorage != value)
                            {
                                _selectedStorage = value;
            
                            }
            
            
                                Storage = "Root Storage: " + _selectedStorage.value;
                               RootFolder=_selectedStorage.value;
                        }
            
                    }
            
    

    Now Can you please tell me how can I assign the value stored in RotFolder variable..

    Ok..I got my requirement done by assigning _storage to RootFolder as below

       public string Storage
            {
    
                get { return _storage??(_storage= RootFolder); }
                set
                {
                    if (_storage != value)
                    {
                        _storage = value;
                        RootFolder = _storage;
                        OnPropertyChanged();
                    }
                }
            }
    
Sign In or Register to comment.