读取和写入Windows Phone 8中的IsolatedStorage列表

Eri*_*rik 5 c# list isolatedstorage windows-phone windows-phone-8

我试图简单地将一个字符串列表保存到Windows Phone 8上的独立存储.我只是创建了一个列表并以这种方式保存:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

List<String> projectList = new List<String>();

projectList.add("yo");

settings.add("yo", projectList");

settings.Save();
Run Code Online (Sandbox Code Playgroud)

如何在应用程序再次启动时从IsolatedStorage读取此数据并将其放入同一列表中?我正在寻找一种简单的方法 - 我没有在网上找到,我没有设法实现我在网上找到的任何解决方案,因为他们使用不同的方法.

请帮忙,非常感谢!

Chr*_*isK 3

您只需将设置投射回列表即可。我通常添加一个辅助函数来读取设置,以防止读取未保存的内容:

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

您可以使用该函数来恢复您的列表,例如如下所示:

List<String> projectList = readSetting("yo") != null ? (List<String>)readSetting("yo") : new List<String>();
Run Code Online (Sandbox Code Playgroud)