簡體   English   中英

如何序列化自定義類型的ObservableCollection

[英]How to serialize an ObservableCollection of custom type

我正在嘗試將自定義類型的ObservableCollection保存到wp8應用程序中的隔離存儲中。 可觀察的集合包含BitmapImage類型和字符串類型的值。 這樣做的目的是從CameraCaptureTask結果中保存圖像及其各自的名稱。

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是將observablecollection綁定到的列表框。

Settings.cs

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

設置是用於從隔離存儲中保存和加載數據的類。

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;
    }
}

此外,可觀察的集合的類型為ImageItem,如下所示

ImageItem.cs

public class ImageItem
{
    public BitmapImage ImageUri
    {
        get;
        set;
    }

    public string ImageName
    {
        get;
        set;
    }
}

由於某些原因,盡管在應用程序運行時,圖片保存在imageList集合中,並填充在imgList列表框中,但是當應用程序關閉並重新啟動時,observablecollection是否為空? 我相信是BitmapImage的問題(未序列化?),我無法獲得可觀察的集合來從CameraCaptureTask保存圖像嗎?

在您的代碼中,我已經看到您正在使用IsolatedStorage類。 我真的不知道如何使用IsolatedStorage類,但是我懷疑給它分配了一個變量

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

不會進行將值保存到變量的設置。 我進行了快速搜索,並在msdn中找到了保存方法,也許可以幫上忙。

但是,您可以縮小問題范圍,僅使用IsolatedStorageSettings以獲得更好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM