【问题标题】:How Do I Update/Save Stepper Value for Later Use?如何更新/保存步进值以供以后使用?
【发布时间】:2019-05-08 00:23:30
【问题描述】:

我有一个单一的窗口应用程序,基本上是一个计算器,我可以在其中动态更改各个条目,以根据刚刚输入的新价格/重量等生成新的利润/盈亏平衡答案。每个条目视图都绑定到一个步进值以进行快速的小调整,或者可以编辑条目以进行大的更改。如何保存步进器当前开启的值,以供下次使用应用程序时使用??

            <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="1.75" />
            </Grid>

我不想每次打开应用程序时都必须检查每个条目并更改值以返回到我离开的位置,就像现在一样......我是编程新手,我可以'找不到任何这样的例子供我学习。我猜我需要以某种方式使用 INotifyPropertyChanged 之类的东西,就像这里 Xamarin Forms - update a label value 一样,如果是这样,我该如何使用步进值来做到这一点?或者有没有更好的方法来获得我想要的结果???

【问题讨论】:

  • 我刚刚意识到你想要这个用于 xamarin 表单,而不是 winforms。您可以使用 Application.Current.Properties 为以后保存值。这已经在这里回答了stackoverflow.com/questions/37266797/…
  • 您想在离开此应用程序之前保存条目中的值吗?并在下次打开应用时显示价值?
  • 华华。入口视图从绑定的步进器值中获取其内容,因此步进器值是我需要保存的。

标签: c# xamarin.forms


【解决方案1】:

您可以使用Xamarin.Essentials,特别是Preferences 类。

据我了解,您希望保存PurchasePriceStepper.Value,以便在重新打开应用程序时检索它。

您可以使用以下代码保存此值:

Preferences.Set("PurchasePriceStepper", PurchasePriceStepper.Value);

您可以使用以下代码检索您保存的值:

var stepperValue = Preferences.Get("PurchasePriceStepper", String.Empty);

请记住,您必须在应用退出时或在步进器ValueChanged 事件中执行值的保存,并且值可能在页面的onAppearing 事件中加载。

【讨论】:

    【解决方案2】:

    如果您想在退出应用程序之前保存一个值并在您再次启动应用程序时显示,您可以使用Xamarin.Essentials NuGet 和“首选项”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2011-11-07
      相关资源
      最近更新 更多