public static async Task SaveFileAsync(string FileName, T data)
{
MemoryStream memStream = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(memStream, data);
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(FileName,
CreationCollisionOption.ReplaceExisting);
using (Stream stream = await file.OpenStreamForWriteAsync())
{
memStream.Seek(0, SeekOrigin.Begin);
await memStream.CopyToAsync(stream);
await stream.FlushAsync();
}
}
public static async Task<T> RestoreFileAsync(string FileName)
{
T result = default(T);
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(FileName);
using (IInputStream inStream = await file.OpenSequentialReadAsync())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
result = (T)serializer.ReadObject(inStream.AsStreamForRead());
return result;
}
}
catch (FileNotFoundException)
{
return default(T);
}
}
これらのメソッドを使用してデータをシリアル化していますが、画像を含むクラスがあります。
[DataMember]
Public Image img{get;set;}
シリーズ化してみます。私は実際に次のことをしています
var thumb = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,
1000, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);
BitmapImage bmg = new BitmapImage();
bmg.SetSource(thumb);
Image img = new Image();
img.Source = bmg;
bitmapImage を自分でシリアル化しようとしましたが、同じ問題です。このエラーが発生し続け、BitmapImage に属性があります。
タイプ 'Windows.UI.Xaml.Media.ImageSource' をシリアル化できません。これを DataContractAttribute 属性でマークし、シリアル化するすべてのメンバーを DataMemberAttribute 属性でマークすることを検討してください。型がコレクションの場合は、CollectionDataContractAttribute でマークすることを検討してください。サポートされているその他の型については、Microsoft .NET Framework のドキュメントを参照してください。