How to store App settings in Xamarin.Forms
Solution 1
It can be done this way too using Properties Dictionary
for storing data:
Application.Current.Properties ["id"] = someClass.ID;
for fetching data:
if (Application.Current.Properties.ContainsKey("id"))
{
var id = Application.Current.Properties ["id"] as int;
// do something with id
}
Solution 2
The Application
object (in VS you get an App
class that inherits from it, I think the Xamarin Studio template might be slightly different) has a Properties
dictionary that is specifically for this. If you need to make sure your properties get saved right away, there is a Application.SavePropertiesAsync
method you can call.
Solution 3
I use Settings Plugin for Xamarin And Windows for Xamarin.Forms
, the upside since this is implemented at the device-level, you can access these settings from any Forms project, PCL library OR your native project within your app.
- Nuget: Xam.Plugins.Settings
private const string UserNameKey = "username_key";
private static readonly string UserNameDefault = string.Empty;
public static string UserName
{
get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}
Solution 4
You can use the preferences included with Xamarin.Essential.
/* You need to using Xamarin.Essentials
If you do not have Xamarin.Essentials installed,
install it from (NuGet Package Manager)
*/
// Add a reference to Xamarin.Essentials in your class
using Xamarin.Essentials;
// To save a value for a given key in preferences
Preferences.Set("my_key", "my_value");
// To retrieve a value from preferences or a default if not set
var myValue = Preferences.Get("my_key", "default_value");
// To check if a given key exists in preferences
bool hasKey = Preferences.ContainsKey("my_key");
// To remove the key from preferences
Preferences.Remove("my_key");
// To remove all preferences
Preferences.Clear();
Have a look at this link: https://docs.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android
Solution 5
If the settings are more of configurations which are not changed at runtime (e.g. sort of web.config for the app) you can use App.xaml and store values in dictionary like that:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="SmallLabel" TargetType="Label">
<Setter Property="TextColor" Value="Gray" />
<Setter Property="Font" Value="{StaticResource FontItalicSmall}" />
</Style>
</ResourceDictionary>
</Application.Resources>
And retrieve them in C# code like that:
var style = (Style)Application.Current.Resources["SmallLabel"];
Comments
-
A. Sinha almost 2 years
How can we store and retrieve App settings as key,value pair in Xamarin.Forms? Like when the app is closed we can store the user preferences and on restarting of the App we are able to get those values.
-
Akash Amin about 8 yearsHow about akavache,have you used that?@SushiHangover
-
SushiHangover about 8 years@AkashAmin Almost all my apps currently have
Akavache
in it (use it for caching but looking to move to Realm for the speed and to remove SQLite dependacy for future projects). But for storing a few app settings it is kind-of over kill ... -
Akash Amin about 8 yearsGreat. I was looking into realm too. Thank You for your feedback though.
-
SushiHangover about 8 years@AkashAmin No problem, I am really interested in the Realm for Xamarin as I've used it once on an iOS Swift-base project and was very impressed, the Xamarin synthetic benchmarks look really promising and as we know speed is king(!) in mobile... but like anything, I'll need to run some tests on the data that I am caching to determine if it really is a better fit...
-
Akash Amin about 8 yearsYes looking forward to it. Thanks.@SushiHangover
-
Andy Dent about 8 yearsNote github.com/akavache/Akavache/pull/287 which we are taking as strong encouragement to finish any Realm features it needs!
-
SushiHangover about 8 years@AndyDent Sweet, sounds great!
-
Emil over 7 years@SushiHangover just that I understand correct. this plugin saves settings per device and if we use Application.Current.Properties, it will be shared for all devices (means for application).
-
SushiHangover over 7 years@batmaci This plugin is different than developer.xamarin.com/guides/xamarin-forms/working-with/…
-
LanderV over 6 yearsBe aware that you shouldn't use this for user settings. These properties are serialized to an xml file and don't use the native settings/preferences mechanism of the underlying platform. E.g. in iOS it will not store items in NSUserDefaults. You won't be able to modify it from the System's Settings Bundle (developer.apple.com/library/content/documentation/Cocoa/…)
-
LanderV over 6 yearsApplication.Current.Properties is more about retaining application state across activations (OnStart/OnSleep/OnResume)
-
phantomraa about 6 yearsFor anyone wondering why their code doesn't persist the properties you set, you need to save them using the above! I pulled a bit of hair out over this one. Thanks!
-
BlueRaja - Danny Pflughoeft about 4 yearsLink is dead and has been moved to github.com/jamesmontemagno/SettingsPlugin. He recommends you use Xamarin.Essentials.Preferences instead, but I did not because I only needed settings, not everything else in Xamarin Essentials.