Xamarin Forms Sharedpreferences cross
Asked Answered
E

2

12

I'd like to know what is the best solution to manipulate application settings in a cross-platform way.

In iOS we can change the settings outside the app in the settings screen, but we don't have that in windows phone and android.

So, my idea is to create a normal page/screen inside the app that shows all my application settings and have an interface with Save() and Get() methods that I can implement specific per device using DependencyServices.

Is this the right way to do it?

Extraterritoriality answered 28/8, 2015 at 10:34 Comment(0)
P
21
  1. The Application subclass has a static Properties dictionary which can be used to store data. This can be accessed from anywhere in your Xamarin.Forms code using Application.Current.Properties.
Application.Current.Properties ["id"] = someClass.ID;

if (Application.Current.Properties.ContainsKey("id"))
{
    var id = Application.Current.Properties ["id"] as int;
    // do something with id
}

The Properties dictionary is saved to the device automatically. Data added to the dictionary will be available when the application returns from the background or even after it is restarted. Xamarin.Forms 1.4 introduced an additional method on the Application class - SavePropertiesAsync() - which can be called to proactively persist the Properties dictionary. This is to allow you to save properties after important updates rather than risk them not getting serialized out due to a crash or being killed by the OS.

https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/app-lifecycle/

  1. Xamarin.Forms plugin which uses the native settings management.

    • Android: SharedPreferences
    • iOS: NSUserDefaults
    • Windows Phone: IsolatedStorageSettings
    • Windows Store / Windows Phone RT: ApplicationDataContainer

https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings

Praxis answered 28/8, 2015 at 12:47 Comment(5)
Thanks for the help. Just one thing, and if I want to have the settings outside the app in iOS and in the others plataforms in the app? Then I need to do what I said before right? Because is more specific, so I can inject or not the settings screen in the app if Device.OS != iOSExtraterritoriality
Yes, you need to check for Device.OS and enable/disable settings page. To have settings outside app (settings menu) you'll need to use second, native approach and don't forget to add Settings.Bundle (useyourloaf.com/blog/…)Praxis
For settings such as string, int, or any other simple data structure I really recommend my settings plugin. I have been using it for over 4 years in all of my applications and it saves to the native management system like Daniel said. Additionally, it works for EVERY type of app built with Xamarin.Forms or traditions. I just did a live recording this morning about it: youtube.com/watch?v=VNPLxeq9ZII&feature=youtu.beEstrange
@DanielLuberda, I would like to call the Application.Current.Properties ["id"] value in my iOS CustomRenderer Class, but it shows Application doesn't contain a definition for Current. I am able to use it in Android CustomRenderer class. It is working fine in Android.Pusan
@Estrange Do you plan an async implementation such as SavePropertiesAsync() in Xamarin.Forms 1.4?Osteopathy
Q
1

I tried using the Application.Current.Properties Dictionary and had implementation problems.

A solution that worked with very little effort was James Montemagno's Xam.Plugin.Settings NuGet. GitHub Installing the NuGet automagically creates a Helpers folder with Settings.cs. To create a persisted setting you do:

    private const string QuestionTableSizeKey = "QuestionTableSizeKey";
    private static readonly long QuestionTableSizeDefault = 0;

and

    public static long QuestionTableSize
    {
        get
        {
            return AppSettings.GetValueOrDefault<long>(QuestionTableSizeKey, QuestionTableSizeDefault);
        }
        set
        {
            AppSettings.AddOrUpdateValue<long>(QuestionTableSizeKey, value);
        }
    }

Access and setting in the app then looks like:

namespace XXX
{
    class XXX
    {
        public XXX()
        {
                long myLong = 495;
                ...
                Helpers.Settings.QuestionTableSize = myLong;
                ...
                long oldsz = Helpers.Settings.QuestionTableSize;                   
        }
    }

}
Qua answered 4/5, 2016 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.