【问题标题】:Xamarin.Essentials:Preferences Isn't Storing Stepper ValueXamarin.Essentials:首选项不存储步进值
【发布时间】:2019-05-09 05:23:34
【问题描述】:

在我的应用程序中,我有多个带有相应条目的步进器来显示每个步进器的当前值。我正在尝试使用 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 包。如果有帮助,我可以尝试提供更多代码。

【问题讨论】:

  • Preferences Isn't Working. What am I doing Wrong? 不是问题的技术描述,也不是在stackoverflow中提出问题的适当方式,请更具体地说明您遇到的问题,您期望发生的事情,正在发生的事情以及抛出的任何错误
  • 我进行了编辑,试图让它更清晰一点,并且不会引发任何错误。
  • 重新打开应用时看到的 Stepper 的默认值是多少,0.01 还是 1.75?
  • 重新打开应用时的默认值为:0.01,这是在 xaml 文件中为步进器值设置的最小值。

标签: c# xamarin xamarin.forms xamarin.essentials


【解决方案1】:

重新打开应用时的默认值为:0.01表示您的绑定不起作用。

我在XamlMainActivity.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()
    {

    }

}

它适用于我。检查您的BindingContextModel 中的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多