Kenzo_Gilead
Kenzo_Gilead

Reputation: 2439

Persistent Storage using Application.Current.Properties not working

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.

Upvotes: 13

Views: 20943

Answers (4)

you can use Xamarin essentials "Preferences" instead:

Preferences.Set("Key", "Value");
Preferences.Get("Key", "Default");

Upvotes: 5

hvaughan3
hvaughan3

Reputation: 11105

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;

Upvotes: 13

user4108368
user4108368

Reputation:

My own problem regarding this issue was due to me not explicitly saving the properties with the following line of code:

Application.Current.SavePropertiesAsync();

Upvotes: 7

Neuxz
Neuxz

Reputation: 336

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/

Upvotes: 1

Related Questions