C# 基本要素:偏好不是';t存储步进值
在我的应用程序中,我有多个带有相应条目的步进器来显示每个步进器的当前值。我正在尝试使用Xamarin.Essentials:首选项保存此值以供以后在应用程序重新打开时使用,但首选项似乎没有在应用程序重新打开时保存步进器值和/或输入所述值 这里是xaml的一个例子C# 基本要素:偏好不是';t存储步进值,c#,xamarin,xamarin.forms,xamarin.essentials,C#,Xamarin,Xamarin.forms,Xamarin.essentials,在我的应用程序中,我有多个带有相应条目的步进器来显示每个步进器的当前值。我正在尝试使用Xamarin.Essentials:首选项保存此值以供以后在应用程序重新打开时使用,但首选项似乎没有在应用程序重新打开时保存步进器值和/或输入所述值 这里是xaml的一个例子 <Grid x:Name="PurchasePriceGrid"> <Grid.ColumnDefinitions> &
<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>
这是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的原因。要点:首选项没有将steppers值保存/输入到条目中。我在所有项目中都实现了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()
{
}
}
它在我这方面起作用。请检查您的
BindingContext
,并且模型中的代码不起作用。我做错了什么?
不是对问题的技术描述,也不是在stackoverflow中提问的适当方式,请更具体地说明您遇到的问题,以及您预期会发生什么,正在发生的事情和正在抛出的任何错误我已进行了编辑,试图使其更清晰一点,并且没有抛出任何错误。当您重新打开应用程序时,您看到的Stepper的默认值是0.01或1.75?当我重新打开应用程序时,默认值是:0.01,这是xaml文件中为steppers值设置的最小值。
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()
{
}
}