locked
How to save an array as a sessionState or roamingSettings values?

    Question

  • I want to store game states in sessionState as well as roamingSettings.

    I've different arrays like:

    var painted = new Array();

    var userChoice = new Array()'

    As user plays the games, the array gets filled:

    like at first click,   painted = {1}

                           and userChoice = {5}

                //Now I want to save this states:

    Windows.Storage.ApplicationData.current.roamingSettings.values[painted] = painted;  //store painted array as it gets  changed. This gives error "this type cannot be saved".

                WinJS.Application.sessionState.paintedArray = painted;  //


    sonal

    Friday, November 1, 2013 7:48 PM

Answers

  • One way to do this is to serialize the array to a string and then save it. Here's the code to save the array and reconstruct the array after reading it.

    // store an Array to the roamingSettings
    var saveArray = new Array();
    saveArray[0] = "foo";
    saveArray[1] = "bar";            
    var stringifiedArray = JSON.stringify(saveArray);
    Windows.Storage.ApplicationData.current.roamingSettings.values["someObject"] = stringifiedArray;
    
                
    // retrieve the Array from roamingSettings
    var retrievedValue = Windows.Storage.ApplicationData.current.roamingSettings.values["someObject"];
    var retrievedArray = JSON.parse(retrievedValue);


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Friday, November 1, 2013 11:21 PM
    Moderator