In my application, I have multiple steppers with corresponding entries to display each stepper's current value. I am attempting to use Xamarin.Essentials:Preferences to save this value for later use when the app is reopened, but Preferences doesn't appear to be saving the stepper value and/or inputting said value when the Application is being reopened.
Here is one example of the xaml...
<Grid x:Name="PurchasePriceGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width=".6*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Label Text="Purchase Price" TextColor="Black" FontAttributes="Bold" Margin="0,0,25,0" Scale="1.2" TranslationX="15"
Grid.Column="0" HorizontalTextAlignment="End" VerticalTextAlignment="Center" LineBreakMode="NoWrap" />
<customentry:MyEntry x:Name="PurchasePriceEntry" Text="{Binding Source={x:Reference PurchasePriceStepper}, Path=Value, FallbackValue=0}"
TextColor="DarkSlateGray" FontAttributes="Bold" BackgroundColor="Ivory" TranslationX="10"
Grid.Column="1" HorizontalTextAlignment="Center" Keyboard="Numeric" VerticalOptions="End" MaxLength="5"
TextChanged="PurchasePriceEntry_Completed" />
<Stepper x:Name="PurchasePriceStepper" Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center" Scale=".7" TranslationX="3"
Maximum="5" Minimum=".01" Increment=".01" Value="{Binding PurchasePriceStepperValue}" />
</Grid>
Here is the C#...
using Xamarin.Essentials;
...
public double PurchasePriceStepperValue
{
get => Preferences.Get(nameof(PurchasePriceStepperValue), 1.75);
set
{
Preferences.Set(nameof(PurchasePriceStepperValue), value);
OnPropertyChanged(nameof(PurchasePriceStepperValue));
}
}
And here is the Android MainActivity.cs...
using Xamarin.Essentials;
...
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
I am new to programming and have spent multiple hours trying multiple different things to try to figure out why Xamarin.Essentials:Preferences isn't saving/inputting the steppers value into the entry. I have implemented the NuGet package in all projects. I can try to give more code if it would help.
The default value when you reopen the app is: 0.01 means your binding is not working.
I use the same code in Xaml
and MainActivity.cs
as yours and use the code behind like below codes:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
PurchasePriceModel model = new PurchasePriceModel();
BindingContext = model;
}
}
class PurchasePriceModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public double PurchasePriceStepperValue
{
get => Preferences.Get(nameof(PurchasePriceStepperValue), 1.75);
set
{
Preferences.Set(nameof(PurchasePriceStepperValue), value);
PropertyChanged(this, new PropertyChangedEventArgs(nameof(PurchasePriceStepperValue)));
}
}
public PurchasePriceModel()
{
}
}
It works on my side.Check your BindingContext
and codes in Model
.