ImageItem
の応答を収集するためのBitmapImageと文字列を含むという名前のカスタムクラスを作成しましたCaptureImageTask
。ObservableCollection
ビューのリストボックスにバインドされている各画像とそれぞれのパスを保存したいと思います。
現在のところ、リストボックスは正しく入力されていますが、分離されたストレージに保存するのに問題ObservableCollection<ImageItem>
がありBitmapImage type
ます。
ソリューションを修正して、BitmapImage
内のそれぞれのパスとともに分離されたストレージに保存できるようにする方法がわかりませんObservableCollection
。
BitmapImage
問題をシリアライズ可能なタイプではないものに絞り込んだと思います。[DataContract]
'[DataMember] attributes within
ImageItem.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; }