I am attempting to save an ObservableCollection of a custom type to isolated storage in my wp8 application. The observable collection holds values of type BitmapImage and string. The purpose of this is to save images and their respective names from the CameraCaptureTask result.
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;
}
Where Settings is a class I have declared to hold the observable collection named imageList, and imgList is a Listbox that the observablecollection is binded to.
Settings.cs
public static class Settings
{
public static readonly Setting<ObservableCollection<ImageItem>> imageList = new Setting<ObservableCollection<ImageItem>>("imageList", new ObservableCollection<ImageItem>());
}
Setting is the class that saves and loads data from isolated storage.
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;
}
}
Additionally, the observable collection is of type ImageItem, which is as follows
ImageItem.cs
public class ImageItem
{
public BitmapImage ImageUri
{
get;
set;
}
public string ImageName
{
get;
set;
}
}
For some reason, although while the application is running pictures are saved in the imageList collection and populated in the imgList Listbox, but when the app is closed and restarted, the observablecollection is empty? I believe the BitmapImage is the issue (it is not serialized?) and I cannot get the observable collection to save the images from CameraCaptureTask?
Setting.cs
to my question above. – Matthew