C# 在IsolatedStorage中保存和获取ObservableCollection时出错
我一直在使用一个助手类,它使用键、值对从独立存储中保存和检索值。这对于在我的应用程序中更改的单个值是有效的,但我现在正尝试将其用于ObservableCollection,其中的项将在运行时添加和删除。由于某些原因,当应用程序关闭并重新打开时,我的ObservableCollection不会存储和/或从IsolatedStorage中检索。我不确定我做错了什么,因为这适用于个人价值观 Setting.csC# 在IsolatedStorage中保存和获取ObservableCollection时出错,c#,windows-phone-7,isolatedstorage,C#,Windows Phone 7,Isolatedstorage,我一直在使用一个助手类,它使用键、值对从独立存储中保存和检索值。这对于在我的应用程序中更改的单个值是有效的,但我现在正尝试将其用于ObservableCollection,其中的项将在运行时添加和删除。由于某些原因,当应用程序关闭并重新打开时,我的ObservableCollection不会存储和/或从IsolatedStorage中检索。我不确定我做错了什么,因为这适用于个人价值观 Setting.cs public class Setting<T> { string na
public class Setting<T>
{
string name;
T value;
T defaultValue;
bool hasValue;
public Setting(string name, T defaultValue)
{
this.name = name;
this.defaultValue = defaultValue;
}
public T Value
{
get
{
//Check for the cached value
if (!this.hasValue)
{
//Try to get the value from Isolated Storage
if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))
{
//It hasn't been set yet
this.value = this.defaultValue;
IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
}
this.hasValue = true;
}
return this.value;
}
set
{
//Save the value to Isolated Storage
IsolatedStorageSettings.ApplicationSettings[this.name] = value;
this.value = value;
this.hasValue = true;
}
}
public T DefaultValue
{
get { return this.defaultValue; }
}
// Clear cached value
public void ForceRefresh()
{
this.hasValue = false;
}
}
TabsPage.xaml.cs
void addNew_Click(object sender, EventArgs e)
{
BitmapImage newTileImage = new BitmapImage();
var newItem = new BrowserItem() { Browser = new FullWebBrowser(), Url = "http://www.bing.com", ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new browser", GroupTag = "TileGroup", TileName = "new" };
newItem.Browser.InitialUri = Settings.InitialUri.Value; //set the initial uri when created
Settings.BrowserList.Value.Add(newItem); //update saved BrowserList
}
在
addNew\u单击
偶数处理程序中,成功添加名为newItem
的新BrowserItem
。但是,当应用程序关闭或重新打开时,我会检查浏览器列表
,查看项目是否存在,如果存在,我想根据ObservableCollection中的项目索引值加载一个特定的项目。每次执行检查时,BrowserList
没有保存的项目?如何在集合中正确保存这些值以使其保持不变?这可能是由于可观察集合中包含的类型序列化失败所致。
首先,需要通过实现序列化接口的继承或应用[serializable]属性将BrowserItem类型标记为可序列化:
[Serializable]
public class BrowserItem
{
}
事实上,如果BrowserItem(或您试图保存到设置的任何其他类型)是可序列化的,那么您可能会得到帮助
另外,如果您的类型已可序列化,则可能需要将集合的样本序列化到xml文件中的磁盘,以便可以手动/直观地检查在序列化/反序列化您的类型时可能导致问题的原因(如果有),我必须在我的项目中引用什么才能使用
Serializable
属性是:System.SerializableAttribute,只要:using System;位于类文件的顶部,您可以使用[Serializable]装饰类定义。好的,我在Windows Phone中使用.NET,因此我必须引用System.Runtime.Serialization,并使用[DataContract]而不是[Serialization]。我在上面添加了我的BrowserItem
自定义类,尽管我不确定是否正确执行了此操作<代码>ObservableCollection仍然无法获取保存的值。做得很好。现在,我认为您需要将bitmapimage属性存储为一个字节数组(字节[]),以便对其进行正确序列化。这可能是它的一部分…您有属性的和/或“FullWebBrowser”类型未标记为可序列化?“FullWebBrowser”是您在某处定义的类吗?如果是,请确保它也可序列化。要将bitmapimage存储为字节,请查看以下内容:
void addNew_Click(object sender, EventArgs e)
{
BitmapImage newTileImage = new BitmapImage();
var newItem = new BrowserItem() { Browser = new FullWebBrowser(), Url = "http://www.bing.com", ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new browser", GroupTag = "TileGroup", TileName = "new" };
newItem.Browser.InitialUri = Settings.InitialUri.Value; //set the initial uri when created
Settings.BrowserList.Value.Add(newItem); //update saved BrowserList
}
[Serializable]
public class BrowserItem
{
}