Saving a list - Windows 8

184 Views Asked by At

I'm developing the Windows 8 equivalent of my app.

I'm trying to save simple list of strings to ApplicationDataContainer, as I would with IsolatedStorage for Windows Phone 8.

In Windows Phone 8 I would do it like this:

List<String> myList;
myList= readSetting("myList") != null ? (List<String>)readSetting("myList") : new List<String>();

Helper method:

        private static object readSetting(string key)
    {
        return IsolatedStorageSettings.ApplicationSettings.Contains(key) ? IsolatedStorageSettings.ApplicationSettings[key] : null;
    }

But how should I do this in Windows 8? My app is of the type Split Page.

Thanks a lot!

Kind Regards, Erik

2

There are 2 best solutions below

1
On

Try using Storage Helpers like this and this. Or you can use StorageFile in Windows 8 which allows you read-write files in local folder

0
On

The equivalent to IsolatedStorageSettings on Win8 (and WP8) is ApplicationData.Current.LocalSettings

Create a container

var container = ApplicationData.Current.LocalSettings.CreateContainer("defaultContainer",ApplicationDataCreateDisposition.Always);
container.Values["newKey"] = "New Value";

Your method would become:

private static object readSetting(string key)
{
    var container = ApplicationData.Current.LocalSettings.CreateContainer("defaultContainer",ApplicationDataCreateDisposition.Existing);
    if (container == null)
    {
       return null;
    }
    return Container.Values[key]
}

Note that this will also work on Windows Phone 8 if you would like to reuse some code between the two platforms.