0
votes

I have a class that stores a ObservableCollection of BitmapImage's and I need to store this information in IsolatedStorage. I am using DataContract to do this with other class's which works. I am thinking that not being able to set the BitmapImage as a DataMember is stopping it from working.

Error message is:

Type 'System.Windows.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.

Any help would be appreciated.

CODE - Save to IsolatedStorage

public static void AddOrUpdateUnsavedItems(string key, List<ProcessClass> unsavedItems)
    {
        var store = IsolatedStorageFile.GetUserStoreForApplication();
        if (store.FileExists("unsaveditem"))
        {
            store.DeleteFile("unsaveditem");
        }
        using (var stream = new IsolatedStorageFileStream("unsaveditem", FileMode.OpenOrCreate, FileAccess.Write, store))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(List<ProcessClass>));
            dcs.WriteObject(stream, unsavedItems);
        }

    }

CODE - Class

[DataContract]
public class ProcessClass
{       
    ObservableCollection<BitmapImage> images = new ObservableCollection<BitmapImage>();
    [DataMember]
    public ObservableCollection<BitmapImage> Images
    {
        get { return images; }
        set { images = value; }
    }
    [DataMember]
    public string Notes { get; set; }
    [DataMember]
    public string ItemID { get; set; }
}
1

1 Answers

0
votes

BitmapImage is simply not serializable using data contract and there is no way to change this by yourself.

The only way to persist a BitmapImage (it's an image) is to use BinaryWriter or WriteableBitmap. An image converter for tranforming BitmapImage to byte[] and vice-versa will help too.