繁体   English   中英

值不在预期范围内,孤立存储

[英]Value does not fall within the expected range, IsolatedStorage

我有一个编写的类,该类将所有对象保存并检索到Windows Phone隔离存储系统。 看一看...

public class DataCache
{
    // Method to store an object to phone ************************************
    public void StoreToPhone(string key, Object objectToStore)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;

        try
        {
            if (existsInStorage(key))
            {
                settings.Remove(key);
                settings.Add(key, objectToStore);
            }
            else
            {
                settings.Add(key, objectToStore);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("An error occured while trying to cache data: " + e.Message);
        }
    }

    // Method to retrieve an object ******************************************
    public Object retrieveFromPhone(string key)
    {            
        var settings = IsolatedStorageSettings.ApplicationSettings;            
        Object retrievedObject = null;

        try
        {
            if (existsInStorage(key))
            {
                settings.TryGetValue<Object>(key, out retrievedObject);
            }
            else
            {
                MessageBox.Show(string.Format("Cannot find key {0} in isolated storage", key));
            }
        }
        catch(Exception e)
        {
            MessageBox.Show("An error occured while trying to retrieve cache object: "+e.Message);
        }
        return retrievedObject;
    }

    // Helper method to check if there is space on the phone to cache the data
    private bool IsSpaceAvailable(long spaceReq)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            long spaceAvail = store.AvailableFreeSpace;
            if (spaceReq > spaceAvail)
            {
                return false;
            }
            return true;
        }
    }

    // Method to check if key exists in isolated storage *********************
    public bool existsInStorage(string key)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        bool objectExistsInStorage = settings.Contains(key);
        return objectExistsInStorage;
    }
}

当我运行我的应用并尝试使用StoreToPhone()方法存储一些数据时,出现以下错误:

尝试缓存数据时发生错误:值不在预期范围内

我不完全知道这是什么意思。是否不期望此类对象? 我不确定...我通过了我写的Fyi自定义类。

有时候我也遇到同样的问题。

似乎错误显示“存储中可能存在重复值”。 所以我在“添加”之前使用了“删除”,然后又发现没有保存所有内容,因此在“添加”之后使用了“保存”功能。

它对我有用...

IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
  appSettings.Remove("name");
  appSettings.Add("name", "James Carter");
  appSettings.Save();
  tbResults.Text = (string)appSettings["name"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM