0

I have created a custom class named ImageItem which contains a BitmapImage and string to gather the response of the CaptureImageTask. I would like to save each image and its respective path to an ObservableCollection which is bound to a listbox in my view.

As of now the listbox populates correctly, but I am having trouble storing ObservableCollection<ImageItem> in isolated storage, I believe because of the BitmapImage type.

I am not sure of how to fix my solution so that the BitmapImage will be allowed to be saved to isolated storage along with its respective path within an ObservableCollection.

I believe I have narrowed down the issue to BitmapImage not being a serializable type. I have tried using [DataContract] and '[DataMember]attributes withinImageItem.cs` without success. I have never attempted to save a nonprimitive type.

Following are the code, with some description of each file.

  • ImageItem.cs

    public class ImageItem
    {
        public BitmapImage ImageUri
        {
            get;
            set;
        }
    
        public string ImagePath
        {
            get;
            set;
        }
    }
    
  • Settings.cs

    I am using a Settings class to create the ObservableCollection of custom type.

    public static class Settings
    {
        public static readonly Setting<ObservableCollection<ImageItem>> imageList = new Setting<ObservableCollection<ImageItem>>("imageList", new ObservableCollection<ImageItem>());
    }
    
  • Setting.cs

    Where Setting is a class that reads and saves data to Isolated Storage

    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;
        }
    }
    
  • MainPage.xaml.cs

    From here I am simply attempting to save the result of the CameraCaptureTask which is used to populate the ObservableCollection and the listbox

    void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            //values declared earlier
            imgChosenPhotoFilePath = null;
            bmp = new BitmapImage();
    
            imgChosenPhotoFilePath = e.OriginalFileName;
    
            bmp.SetSource(e.ChosenPhoto);
            imgChosenPhoto.Source = bmp;
    
            //Add photo to listbox and observablecollection
            AddToImgList(imgChosenPhotoFilePath, bmp);
        }
    }
    
    private void AddToImgList(string filePath, BitmapImage bitmap)
    {
        //save the values to the ObservableCollection
        Settings.imageList.Value.Add(new ImageItem() { ImagePath = filePath, ImageUri = bitmap });
    
        //populate the listbox in the view named imgList
        imgList.ItemsSource = Settings.imageList.Value;
    }
    
4

1 回答 1

0

正如您所发现的,您无法序列化BitmapImage. 最简单的替代方法是将其保存为单独的文件,然后将文件名保存在您正在序列化的集合中。

显然,在反序列化时,您需要从磁盘读取文件并将其加载回 BitmapImage。

如果您要在应用程序的整个生命周期中保留这些数据以供使用,那么将图像直接保存到 IsolatedStorage 并将路径保留在视图模型中可能会更容易。然后,您可以将路径绑定到 ListBoxItemTemplate 中的图像。

于 2013-03-22T01:54:49.487 回答