C# 如何序列化自定义类型的ObservableCollection
我试图将自定义类型的ObservableCollection保存到wp8应用程序中的独立存储中。可观察集合保存BitmapImage和string类型的值。其目的是保存CameraCaptureTask结果中的图像及其各自的名称C# 如何序列化自定义类型的ObservableCollection,c#,serialization,windows-phone-8,observablecollection,C#,Serialization,Windows Phone 8,Observablecollection,我试图将自定义类型的ObservableCollection保存到wp8应用程序中的独立存储中。可观察集合保存BitmapImage和string类型的值。其目的是保存CameraCaptureTask结果中的图像及其各自的名称 void cameraCaptureTask_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { /
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//clear current values if any
imgChosenPhotoFileName = null;
bmp = new BitmapImage();
imgChosenPhotoFileName = e.OriginalFileName;
//Display the photo on the page
bmp.SetSource(e.ChosenPhoto);
imgChosenPhoto.Source = bmp;
//Add photo info to Recent
AddToImgList(imgChosenPhotoFileName, bmp);
}
}
private void AddToImgList(string fileName, BitmapImage bitmap)
{
Settings.imageList.Value.Add(new ImageItem() { ImageName = fileName, ImageUri = bitmap });
//Settings.imageList.Value.Add(bitmap);
//populate the List with the saved imageList
imgList.ItemsSource = Settings.imageList.Value;
}
其中,Settings是我声明用来保存名为imageList的可观察集合的类,imgList是可观察集合绑定到的列表框
设置.cs
public static class Settings
{
public static readonly Setting<ObservableCollection<ImageItem>> imageList = new Setting<ObservableCollection<ImageItem>>("imageList", new ObservableCollection<ImageItem>());
}
出于某种原因,虽然在应用程序运行时,图片保存在imageList集合中并填充在imgList列表框中,但当应用程序关闭并重新启动时,observablecollection为空?我认为BitmapImage是问题所在,它是否未序列化?我无法从CameraCaptureTask获取可观察集合来保存图像?在代码中,我看到您使用的是IsolatedStorage类。 我真的不知道如何使用IsolatedStorage类,但是我怀疑为它分配一个变量 IsolatedStorageSettings.ApplicationSettings[this.name]=this.value 不进行将值保存到变量中的设置。我已经做了一个快速搜索,并在找到了一个保存方法,也许它可以帮助
但是,您可以将问题缩小到仅使用IsolatedStorage设置来获得更好的答案。您可以将加载/保存代码添加到问题中吗?这似乎是问题的根源…根据我的经验,ObservableCollection应该与List没有任何不同。我怀疑问题出在save/load方法之间,或者ImageItem类无法序列化。很抱歉,以前从未尝试序列化BitmapImage,因此我怀疑它。是的,我认为BitmapImage的序列化或保存/加载方法是问题的根源,但我不确定如何解决此问题。我在上面的问题中添加了名为Setting.cs的保存/加载方法。
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;
}
}
public class ImageItem
{
public BitmapImage ImageUri
{
get;
set;
}
public string ImageName
{
get;
set;
}
}