0

ImageItemの応答を収集するためのBitmapImageと文字列を含むという名前のカスタムクラスを作成しましたCaptureImageTaskObservableCollectionビューのリストボックスにバインドされている各画像とそれぞれのパスを保存したいと思います。

現在のところ、リストボックスは正しく入力されていますが、分離されたストレージに保存するのに問題ObservableCollection<ImageItem>がありBitmapImage typeます。

ソリューションを修正して、BitmapImage内のそれぞれのパスとともに分離されたストレージに保存できるようにする方法がわかりませんObservableCollection

BitmapImage問題をシリアライズ可能なタイプではないものに絞り込んだと思います。[DataContract]'[DataMember] attributes withinImageItem.cs`を使用してみましたが成功しませんでした。非プリミティブタイプを保存しようとしたことはありません。

以下は、各ファイルの説明とともにコードです。

  • ImageItem.cs

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

    Settingsクラスを使用してカスタムタイプのを作成していますObservableCollection

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

    Settingデータを読み取って分離ストレージに保存するクラスはどこにありますか

    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

    ここから、とリストボックスにデータCameraCaptureTaskを入力するために使用されるの結果を保存しようとしています。ObservableCollection

    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 に答える