none
IsolatedStorageSettings to Save Application Settings

    Question

  • I'm currently trying to implement a little helper class for IsolatedStorage and am running into some difficulty.

    Here is my class so far:

    public class LocalStorageHelper
    {
        private const string KEY_FOO_LIST = "FooList";

        public static List<FooItem> GetFooFromLocalStorage()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(KEY_FOO_LIST))
                return IsolatedStorageSettings.ApplicationSettings[KEY_FOO_LIST] as List<Foo>;

            return new List<FooItem>();
        }

        public static void UpdateLocalStorageFoo(List<Foo> listFooItems)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(KEY_FOO_LIST))
                IsolatedStorageSettings.ApplicationSettings[KEY_FOO_LIST] = listFooItems;
            else
               
    IsolatedStorageSettings.ApplicationSettings.Add(KEY_FOO_LIST, listFooItems);
        }
    }

    It's my understanding that the local storage should persist at least for the duration of the user's visit to the site.  There is a tab in the application that, when clicked, displays a list of items from local storage.  When I bind this list of items I use the GetFooFromLocalStorage() method above.  If I want to add / remove items I create a local list, add / remove the item, and then save to local storage.

    If I press F5 (refresh the page) it appears that the items are gone.  I have set breakpoints in code and see the count of List<Foo> is 0 when I just recently added 2 items to the local storage.

    Am I doing this right? This example is based off of a video I watched on Channel9.

    Thanks in advance.

    Monday, August 18, 2008 5:11 PM

Answers

  • If I press F5 (refresh the page) it appears that the items are gone.

     

    Hi morrissey,

    You didn't save the content of ApplicationSettings. Use the following at the end of the update method:  

                            IsolatedStorageSettings.ApplicationSettings.Save();

    This will save the contents. Try this & let me know.

     

    Wednesday, August 20, 2008 12:06 AM

All replies

  • I think this blog post is a bit closer to what you are trying to do. Watch the sample code carefully:

    http://www.imason.com/blogs/jim_schwartz/archive/2008/07/03/isolated-storage-in-silverlight-2-beta-2.aspx

    Monday, August 18, 2008 7:36 PM
  • Thanks Peter.

    I have implemented as the code displayed but it doesn't seem to persist for more than the current session.  I was under the impression that I could shut my browser down, re-open and the settings would persist.

    Maybe it's just the application I'm working with.

    Tuesday, August 19, 2008 2:56 PM
  • I've used IsolatedStorage in several SL apps, and it definitely persists beyond a single session. Suggest that you review your code carefully, you may accidentally be "shooting yourself in the foot".

     

    Tuesday, August 19, 2008 3:16 PM
  • You're probably right Peter ;) I'm going to post some code so that someone may see what I'm doing wrong.

    public class LocalStorage
    {
        public static object GetObjectFromLocalStorageKey(string key)
        {
            try
           
    {
                return IsolatedStorageSettings.ApplicationSettings[key];
            }
            catch (KeyNotFoundException)
            {
                return null;
            }
        }

        public static void UpdateLocalStorageObject(string key, object value)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                IsolatedStorageSettings.ApplicationSettings[key] = value;
            else
               
    IsolatedStorageSettings.ApplicationSettings.Add(key, value);

            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }

    ---

    using System.Runtime.Serialization;

    namespace LocalStorage
    {
        [DataContract]
        public class FrameworkUser
       
    {
            [DataMember]
            public MediaFrameworkUser CurrentFrameworkUser { get; set; }
        }
    }

    ---

    Here is the helper class that I use to retrieve the User:

    public static FrameworkUser GetCurrentUserFromLocalStorage()
    {
        try
       
    {
            LocalStorage.FrameworkUser user = LocalStorage.GetObjectFromLocalStorageKey(KEY_CURRENT_USER) as LocalStorage.FrameworkUser;

            return user != null ? user.CurrentFrameworkUser : null;
        }
        catch (KeyNotFoundException)
        {
            return null;
        }
    }

    Here's the helper method that stores / saves (or is supposed to!) the current user:

    public static void UpdateLocalStorageCurrentUser(MediaFrameworkUser frameworkUser)
    {
        LocalStorage.FrameworkUser user = new LocalStorage.FrameworkUser {CurrentFrameworkUser = frameworkUser};
        LocalStorage.UpdateLocalStorageObject(KEY_CURRENT_USER, user);
    }

    Tuesday, August 19, 2008 3:32 PM
  • Morrissey-

    Your code for storing and retrieving values from ApplicationSettings looks fine. Try testing with a simple string value to ensure that it's not a serialization issue with your FrameworkUser class that's causing the trouble.

    Also, keep in mind that AppSettings isn't the "most" persistent place you can store data with Silverlight. It is only scoped per browser (and in the case of AppSettings, per SL app), so someone that accesses a site via IE and Firefox on the same computer will not have access to the data in ApplicationSettings. Values stored in the UserStore, though, are available across browsers on the client. Alternatively, values stored in SiteSettings (vs ApplicationSettings) should be available across all SL apps on a domain, which may also be more appealing. Options to consider.

    -Todd

    Tuesday, August 19, 2008 4:53 PM
  • @Todd: I tried your suggestion using SiteSettings and setting a simple string.

    string testing = LocalStorage.GetObjectFromLocalStorageKey("TEST_STRING") as string;

    if (string.IsNullOrEmpty(testing))
        LocalStorage.UpdateLocalStorageObject("TEST_STRING", "TESTING");
    else
       
    Console.WriteLine(testing);

    The testing variable was null everytime.  I'd be lying if I said I was having fun right now.

    Tuesday, August 19, 2008 5:50 PM
  • If I press F5 (refresh the page) it appears that the items are gone.

     

    Hi morrissey,

    You didn't save the content of ApplicationSettings. Use the following at the end of the update method:  

                            IsolatedStorageSettings.ApplicationSettings.Save();

    This will save the contents. Try this & let me know.

     

    Wednesday, August 20, 2008 12:06 AM
  • created a static helper class for using isolated storage if anyone is interested.

    http://www.dansoltesz.com/post/2010/04/06/silverlight-save-settings.aspx

     

    Remember: Please click on "Mark As Answer", if this answered your query partially or fully.

    Tuesday, April 06, 2010 12:59 PM
  • Hi Dan,

    It's a very nice post & hope the community will get easy access to the IsolatedStorage. Cheers.

    Thursday, April 08, 2010 4:19 AM
  • Ahhh!!!! A small suggestion will be to add some validation logic to check whether the appSettings is null and then doing the operation as needed.

    Thursday, April 08, 2010 4:21 AM