【问题标题】:Store a Bitmap image on Windows Phone 8在 Windows Phone 8 上存储位图图像
【发布时间】:2014-04-05 20:44:17
【问题描述】:

我会存储一个元素列表,但是当我运行我的应用程序时会显示异常

这个异常的文本:

无法序列化类型“System.Windows.Media.Imaging.WriteableBitmap”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。或者,您可以确保该类型是公共的并且有一个无参数的构造函数 - 然后该类型的所有公共成员都将被序列化,并且不需要任何属性。

Element el = new Element();
el.Text = TextACoder.Text;
el.Date = DateTime.Now.ToShortDateString();
el.SourceImg = ImageQR.Source;

List<Element> ListElement = new List<Element>();

if(IsolatedStorageSettings.ApplicationSettings.Contains("ListEl"))
{
    ListElement = (List<Element>)IsolatedStorageSettings.ApplicationSettings["ListEl"];
    ListElement.Add(el);
    IsolatedStorageSettings.ApplicationSettings["ListEl"] = ListElement;
    IsolatedStorageSettings.ApplicationSettings.Save();
}
else
{
    ListElement.Add(el);
    IsolatedStorageSettings.ApplicationSettings["ListEl"] = ListElement;
    IsolatedStorageSettings.ApplicationSettings.Save();
}            

【问题讨论】:

    标签: c# windows-phone


    【解决方案1】:

    当您分配给IsolatedStorageSettings.ApplicationSettings 时,它会在后台序列化数据以将其保存到IsolatedStorageFile,并且异常抱怨它不知道如何序列化WriteableBitmap 类。您可以改为存储 WriteableBitmap 数据的字节数组 - 这里是 how you can convert a WriteableBitmap to a byte[] and back again(请注意,您必须分别存储宽度和高度)。

    根据您正在做的事情,您可以将byte[] 字段添加到您的班级,并且每当您分配到SourceImg 时,也可以更新 byte[] 字段。然后用NonSerialized 属性标记支持SourceImg 属性的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多