在我的应用程序中,我有多个带有相应条目的步进器来显示每个步进器的当前值。我正在尝试使用 Xamarin.Essentials:Preferences 保存此值以供重新打开应用程序时使用,但在重新打开应用程序时,Preferences 似乎没有保存步进值和/或输入所述值。
这是 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>
这里是 C#...
using Xamarin.Essentials;
...
public double PurchasePriceStepperValue
{
get => Preferences.Get(nameof(PurchasePriceStepperValue), 1.75);
set
{
Preferences.Set(nameof(PurchasePriceStepperValue), value);
OnPropertyChanged(nameof(PurchasePriceStepperValue));
}
}
这是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);
}
我是编程新手,花了几个小时尝试多种不同的事情来试图弄清楚为什么 Xamarin.Essentials:Preferences 没有将步进器值保存/输入到条目中。我已经在所有项目中实现了 NuGet 包。如果有帮助,我可以尝试提供更多代码。