Question

I want to store some basic information to prevent login each time for that I am using IsolatedStorageSettings but data is stored only till application is open how can I persist data even when application is closed and get back when application is started again. My code is as below to store information I uses

public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("user", MainPage.user);

to retrive it I use

if (MainPage.settings.Contains("user"))
{
      MainPage.user = (User)MainPage.settings["user"];                            
}

here MainPage.user is a static object of class User in class MainPage.

Was it helpful?

Solution

You need to call the Save method:

settings.Save();

OTHER TIPS

I have two assumptions here:

1) You don't call settings.Save() method

2) You're working on emulator and close it, that'll cause all your settings to die.

Your user class should look like this:

[DataContractAttribute]
public class User
    {
        [DataMemberAttribute]
        private String field;

        public String _Field
        {
            get { return field; }
            set { field = value; }
        }
    }

just set [DataMemberAttribute] for every field you want to save.

You can use this code to Add Settings:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("user", "SOME_STRING_DATA");

To Update Settings:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings["user"] = "some_string_content";

To Retrieve Settings:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
string value = (string)appSettings["user"];

This works without using Save() function.

I hope this is what you were looking for.

Seems like every thing OK in your code expect below line of code. Remove static when you define IsolatedStorageSettings object. your code should be...

public IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

and make following changes in your app.xaml.cs file

 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
           // SettingView.LiveClient = null;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        // Code to execute when the application is closing (eg, user hit Back)
        // This code will not execute when the application is deactivated
        private void Application_Closing(object sender, ClosingEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
        } 

IsolatedStorageSettings stores data in a 'key value' pair and to save it use IsolatedStorageSettings.Save() Method:

var lastOpenedFileSettings = IsolatedStorageSettings.ApplicationSettings;
  if (!lastOpenedFileSettings.Contains("yourSeetingKey"))
       {
         lastOpenedFileSettings.Add("yourSeetingKey", yourSettingValue);
        }
   IsolatedStorageSettings.ApplicationSettings.Save();          

To retrive setting value cast it into required datatype:

IsolatedStorageSettings lastOpenedFileSettings =IsolatedStorageSettings.ApplicationSettings;
 string lastOpenedFileId = (string)lastOpenedFileSettings["yourSeetingKey"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top