如何从IsolatedStorage设置中获取字节值?

时间:2012-01-23 17:26:31

标签: silverlight windows-phone-7

喜欢标题我想要做的是从IsolatedStorageSettings获取字节(类型)值

IsolatedStorageSettings isoSett = IsolatedStorageSettings.ApplicationSettings;
        if (isoSett.Contains("level") && isoSett.Contains("sound"))
        {
            bool dzwiek = (bool)isoSett["sound"];
            byte poziom = (byte)isoSett["level"];// here i get InvalidCastException
        }
        else 
        {
            isoSett.Add("level", 1);
            isoSett.Add("sound", true);
            isoSett.Save();
        }

我应该如何追溯这个价值? 谢谢你提前:))

编辑:此InvalidCastException下没有其他内容写入

2 个答案:

答案 0 :(得分:2)

看看这是否适合你:

从隔离存储中保存通用对象

public void Save<T>(string fileName, T item)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(fileStream, item);
        }
    }
}

从隔离存储加载通用对象

public T Load<T>(string fileName)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            return (T)serializer.ReadObject(fileStream);
        }
    }
}

调用加载

string fileName = "SampleTest.txt";
byte level = 1;
Save<byte>(fileName, level);
byte value = Load<byte>(fileName);

答案 1 :(得分:1)

你应该深入研究调试器,看看isoSett["level"] 实际上是什么

假设它是你保存它的方式,但这不应该导致该转换失败。除非level是一个更大的数字并且你溢出了字节?