将隔离存储设置从WP8升级到WP8.1

本文关键字:WP8 隔离 存储 设置 | 更新日期: 2023-09-27 18:22:04

我有一个windows phone 8应用程序,我想升级到WP8.1通用应用程序。8.1中不支持独立存储,在这种情况下如何升级我的独立设置?

将隔离存储设置从WP8升级到WP8.1

ApplicationData.LocalSettings--获取本地应用程序数据存储中的应用程序设置容器。

命名空间:Windows.Storage

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
// Create a simple setting
localSettings.Values["exampleSetting"] = "Hello Windows";
// Read data from a simple setting
Object value = localSettings.Values["exampleSetting"];
if (value == null)
{
    // No data
}
else
{
    // Access data in value
}
// Delete a simple setting
localSettings.Values.Remove("exampleSetting");

您可以在这里阅读文档

当应用程序通过存储更新时,包含IsolatedStorageSettings的__ApplicationSettings文件将位于应用程序的本地文件夹中
当通过Visual Studio更新应用程序时,情况并非如此,因为用WinRT应用程序替换Silverlight应用程序似乎有困难。

您需要将该文件反序列化为任何已知的对象,并将对象存储在其他地方。以下代码将在WinRT应用程序中获取IsolatedStorageSettings:

    public static async Task<IEnumerable<KeyValuePair<string, object>>> GetIsolatedStorageValuesAsync()
    {
        try
        {
            using (var fileStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("__ApplicationSettings"))
            {
                using (var streamReader = new StreamReader(fileStream))
                {
                    var line = streamReader.ReadLine() ?? string.Empty;
                    var knownTypes = line.Split(''0')
                        .Where(x => !string.IsNullOrEmpty(x))
                        .Select(Type.GetType)
                        .ToArray();
                    fileStream.Position = line.Length + Environment.NewLine.Length;
                    var serializer = new DataContractSerializer(typeof (Dictionary<string, object>), knownTypes);
                    return (Dictionary<string, object>) serializer.ReadObject(fileStream);
                }
            }
        }
        catch (FileNotFoundException)
        {
            // ignore the FileNotFoundException, unfortunately there is no File.Exists to prevent this
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
        return new Dictionary<string, object>();
    }