在我的应用程序中,我有多个分步器和相应的条目来显示每个步骤的当前值。我试图使用Xamarin.Essentials: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));
}
}这是机器人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包。如果有帮助的话,我可以尝试提供更多的代码。
发布于 2019-05-10 01:37:41
重新打开应用程序时的默认值是: 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中的代码。
https://stackoverflow.com/questions/56052701
复制相似问题