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);
}
}
I'm using these methods to serialize my data, but i have a class that contains an image,
[DataMember]
Public Image img{get;set;}
I'm trying to serialize it. I'm doing the following actually
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;
i tried to serialize the bitmapImage it self but it is the same problem. i keep getting this error, and my BitmapImage has an attribute.
Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.