3

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.

3
  • do you use real device or emulator?
    – Olter
    Mar 4, 2014 at 10:15
  • 1
    Do you use JSON data? If yes, do you deserialize it properly before saving?
    – Olter
    Mar 4, 2014 at 11:01
  • Yes I have used json and I can serialize and deserialize it! so now I have no need to use DataContractAttribute and settings.save() works! Thanks for your support!
    – Kevan
    Mar 4, 2014 at 13:29

5 Answers 5

3

You need to call the Save method:

settings.Save();
3
  • I am getting this error in using settings.save() Type 'vUrja_Mobile_Json.Device' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
    – Kevan
    Mar 4, 2014 at 10:25
  • @Kevan: here is an article that explains what you want to do in details: mobile.dzone.com/articles/windows-phone-how-easily-store Mar 4, 2014 at 10:43
  • This is also usefull Thanks!
    – Kevan
    Mar 4, 2014 at 13:30
0

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.

7
  • If you can't save a class, than you should mark the class with [DataContractAttribute] or [DataContract] and the fields, you want to save with [DataMemberAttribute] or [DataMember].
    – Olter
    Mar 4, 2014 at 10:31
  • I am to closing emulator each time I am getting this error in using settings.save() Type 'vUrja_Mobile_Json.Device' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
    – Kevan
    Mar 4, 2014 at 10:32
  • can you please tell me in which assembly this attribute is given because I am getting this error. The type or namespace name 'DataContractAttribute' could not be found (are you missing a using directive or an assembly reference?)
    – Kevan
    Mar 4, 2014 at 10:37
  • System.Runtime.Serialization
    – Olter
    Mar 4, 2014 at 10:38
  • btw, writing DataContractAttribute in Google would give you the right assembly much quicker.
    – Olter
    Mar 4, 2014 at 10:38
0

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.

2
  • That looks like almost the same code as OP has written. Can't see any difference.
    – Olter
    Mar 4, 2014 at 10:54
  • He has mentioned using settings.Save(); and I have no where mentioned that. Mar 4, 2014 at 11:00
0

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();
        } 
0

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"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.