1
votes

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?

1
Could you add your load/save code to your question? It seems to be the root of the problem...Roman Gruber
As per my experience, ObservableCollection<T> should not have any different with List<T>. I suspect the problem is in between your save/load method, or that your ImageItem class cannot be serialized. Sorry, never tried to serialize a BitmapImage before, so I suspected it.Fendy
Yes, I believe that either theserialization of the BitmapImage or the save/load method is the root of the problem, but I am not sure as how to fix this issue. I added my save/load method named Setting.cs to my question above.Matthew

1 Answers

0
votes

In your code, I have seen that you are using IsolatedStorage class. I don't really know about using the IsolatedStorage class, however I suspect that assigning a variable to it

IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;

does not makes the setting to save the value into variable. I have do a quick search and found a save method at msdn, maybe it can help.

However, you can narrow your question to only use IsolatedStorageSettings to get better answer.