Xamarin.Essentials:首选项不存储步进器值
Xamarin.Essentials:Preferences Isn't Storing Stepper Value
在我的应用程序中,我有多个步进器和相应的条目来显示每个步进器的当前值。我正在尝试使用 Xamarin.Essentials:Preferences 来保存此值以供日后重新打开应用程序时使用,但 Preferences 似乎没有保存步进值 and/or 在应用程序正在输入时输入所述值重新开放。
这是 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 不是 saving/inputting 步进器值进入条目。我已经在所有项目中实施了 NuGet 包。如果有帮助,我可以尝试提供更多代码。
重新打开应用程序时的默认值为:0.01表示您的绑定无效。
我在 Xaml
和 MainActivity.cs
中使用与您相同的代码,并像下面的代码一样使用后面的代码:
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()
{
}
}
它适用于我的 side.Check 你的 BindingContext
和 Model
中的代码。
在我的应用程序中,我有多个步进器和相应的条目来显示每个步进器的当前值。我正在尝试使用 Xamarin.Essentials:Preferences 来保存此值以供日后重新打开应用程序时使用,但 Preferences 似乎没有保存步进值 and/or 在应用程序正在输入时输入所述值重新开放。
这是 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 不是 saving/inputting 步进器值进入条目。我已经在所有项目中实施了 NuGet 包。如果有帮助,我可以尝试提供更多代码。
重新打开应用程序时的默认值为:0.01表示您的绑定无效。
我在 Xaml
和 MainActivity.cs
中使用与您相同的代码,并像下面的代码一样使用后面的代码:
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()
{
}
}
它适用于我的 side.Check 你的 BindingContext
和 Model
中的代码。