我有一个我编写的类,可以将任何对象保存并检索到 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() 方法存储一些数据时,我收到以下错误:
尝试缓存数据时出错:值不在预期范围内
我不完全知道这意味着什么。它不期待这种类型的对象吗?我不确定......我正在传递一个我写的自定义类,仅供参考。