1

I have a single window application, basically a calculator, where I can change individual entries on the fly to generate a new profit/break-even answer based on the new price/weight etc. just entered. Each entry view is bound to a stepper value for quick small adjustments or the entry can be edited for big changes. How do I save the value, that the stepper is currently on, for use the next time the application is used??

            <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>

I don't want to have to go through each entry and change the values to get back to where I left off every time I open the application, as it does now... I am new to programming and I can't find any example of this for me to follow. I'm guessing I need to somehow use something like INotifyPropertyChanged like here Xamarin Forms - update a label value and if so how do I do that with a stepper value? Or is there a better way to get my desired result???

StevenB
  • 39
  • 1
  • 10
  • You want to save the value in entry before you leave this app? And show the value next time you open the app? – nevermore May 08 '19 at 02:29
  • I just realized you wanted this for xamarin forms, not winforms. You can save values for later by using Application.Current.Properties. This has already been answered here https://stackoverflow.com/questions/37266797/how-to-store-app-settings-in-xamarin-forms – Versex May 08 '19 at 00:43
  • Jack Hua. The entry view gets its content from the binded stepper value, so the stepper value is what I am needing to save. – StevenB May 08 '19 at 22:04

2 Answers2

1

If you want to save a value before you exit from app and show when you launch the app again you can use Xamarin.Essentials NuGet and the "Prefrences".

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Akhil George
  • 124
  • 5
1

You could use Xamarin.Essentials and specifically Preferences class.

As I understood, you want to save PurchasePriceStepper.Value in a way to retrieve it when you reopen the application.

You can save this value with the following code:

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

You can retrieve your saved value with the following code:

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

Keep in mind that you must perform the save of the values on app exit, or in stepper ValueChanged event and the values load maybe in the onAppearing event of the page.

GeralexGR
  • 2,973
  • 6
  • 24
  • 33