Stack Overflow Asked by Alloylo on December 11, 2021
I want to save the state of a switch in Xamarin.forms even when the user close the application, I followed the Microsoft tutorial about the Xamarin essentials preferences API tutorialXamarin.Essentials: Preferences. Below is the switch code in Xaml and Xaml.cs
<StackLayout Orientation="Horizontal" IsVisible="True" Margin="50,10">
<Label Text="Remember Me:" />
<Switch IsToggled="{Binding SwitchMe}" />
</StackLayout>
public bool SwitchMe
{
get => Preferences.Get(nameof(SwitchMe), false);
set
{
Preferences.Set(nameof(SwitchMe), value =true);
OnPropertyChanged(nameof(SwitchMe));
}
}
Problem:
The switch state will return to its defaults when the application is closed.
Is there a problem in my code ?
I have added all the requirements in the android and ios for xamarin.essentials.
Thank you guys, the solution was like the following:
public bool SwitchMe
{
get => Preferences.Get(nameof(SwitchMe), false);
set
{
Preferences.Set(nameof(SwitchMe), value);
PropertyChanged(this, new PropertyChangedEventArgs(nameof(SwitchMe)));
}
Answered by Alloylo on December 11, 2021
Reference Xamarin.Essentials.Preferences
directly from the Property in your ViewModel:
public bool SwitchMe
{
get => Preferences.Get(nameof(SwitchMe), false);
set
{
Preferences.Set(nameof(SwitchMe), value);
OnPropertyChanged(nameof(SwitchMe));
}
}
Answered by Ramon Farizel on December 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP