Persistent Storage using Application.Current.Properties not working
Solution 1
I have had a ton of problems with Application.Current.Properties
on Android. I highly suggest using Xamarin Settings plugin instead which I have never had any issues with. It is persistent even when the app is closed.
That being said Application.Current.Properties
is supposed to work even when you close the app. Not sure why it wouldn't but it does not surprise me either.
*Edit: To use once it is installed, basically CrossSettings.Current
is the plugin class that will do the work but the example just creates a separate property to access it. So create a new file, lets call it SettingsImplementation:
public static class SettingsImplementation {
#region Instance
private static Lazy<ISettings> _appSettings;
public static ISettings AppSettings {
get {
if(_appSettings == null) {
_appSettings = new Lazy<ISettings>(() => CrossSettings.Current, LazyThreadSafetyMode.PublicationOnly);
}
return _appSettings.Value;
}
set {
_appSettings = new Lazy<ISettings>(() => value, LazyThreadSafetyMode.PublicationOnly);
}
}
#endregion
private const string UserNameKey = "username_key"; //Key used to get your property
private static readonly string UserNameDefault = string.Empty; //Default value for your property if the key-value pair has not been created yet
public static string UserName {
get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}
}
Then to use that you would do this anywhere in your app:
SettingsImplementation.UserName = "something";
OR
string username = SettingsImplementation.UserName;
Solution 2
you can use Xamarin essentials "Preferences" instead:
Preferences.Set("Key", "Value");
Preferences.Get("Key", "Default");
Solution 3
I ran into the same issue.
The problem:
I was trying to throw complex objects into the Application Properties.
It turns out that the Properties can only take primitive data typs.
This Blog was very helpfull.
https://codemilltech.com/persist-whatever-you-want-with-xamarin-forms/
![Elias MP](https://i.stack.imgur.com/oSvjP.jpg?s=256&g=1)
Elias MP
What can I say? I truly enjoy IT, it all began as a hobby to finally become my career and life. I have worked in different fields within this sector, from support throughout systems, until I found my place in the field of development. I love What I do. :)
Updated on June 05, 2022Comments
-
Elias MP about 2 years
I'm trying to achieve a persistent storage in Xamarin.Forms. After researching in Xamarin.Forms, I decided to use
Application.Current.Properties
property.It looks like it is working just only if the app still remains alive. If I close the app and start it again the
Application.Current.Properties
is empty.Does anyone know if I'm doing something wrong? Can I achieve this feature in another way?
As usual, thanks guys.
-
Elias MP almost 8 yearsHi @hvaughan3, first at all thanks for the answer. It looks like will work for me. An stupid question, I can´t use the plugin following the manual... Can you give me a hand?
-
Elias MP almost 8 yearsI installed the package, and then I added it to each solution, but I dont know what I´m doing wrong. I can´t use AppSettings.GetValueOrDefault...
-
Elias MP almost 8 yearspublic static class Settings is created in my rootnamespace.ViewModel.Helpers but Visual don´t recognize the class...
-
hvaughan3 almost 8 years@EliasMP Sorry, just seeing this. Let me edit my answer.
-
Elias MP almost 8 yearsThanks mate for helping me. :)
-
hvaughan3 almost 8 years@EliasMP Np. Please see edit. I think those
Plugin.Settings
namespaces are correct but I could have them backwards. Let me know if you still get errors -
Elias MP almost 8 yearsIt works! Even when I turn on again the app. Thanks @hvaughan3. Glad to meet you today. :)))))
-
iBobb over 3 yearsIt is recommended, to include some information from links providing solutions. The main point here is: to go around the fact that you can only save primitive types such as strings in
Application.Current.Properties
you can serialize your object into aJSON
string and save it like that :) -
ho h over 3 yearsWhat should be written inside
"something"
? AndSettingsImplementation.UserName
where to use? -
hvaughan3 over 3 years
something
is any unique key that you want to use to save your value. Each value you want to save will need its own unique key. You would useSettingsImplementation.UserName
where ever you need to use that saved value. If you are saving a user's username, then maybe you use it to prefill the login username field or you display in on their profile page. When they want to change their username you assign the new value to itSettingsImplementation.UserName = "MyNewUserName"