我正在使用带有图像和文本块的ListBox的Windows phone应用程序 . 这里的图像位于应用程序的 IsolatedStorage
中 . 要在 ListBlock
中显示图像,我正在转换为流 . 在此之后我使用 DataContractSerializer
在Isolatedstorage中序列化和存储 .
第1页中的代码:
public BookDataList listobj = new BookDataList();
public class BookDataList : List<BookData>//for storing BookData class items with type of list
{
}
[DataContract]
public class BookData
{
[DataMember]
public BitmapImage ImageBinary
{
get { return m_ImageBinary; }
set { m_ImageBinary = value; }
}
[DataMember]
public BitmapImage m_ImageBinary;
public BookData(string strImageName)
{
//*** Image Binary ***'
BitmapImage image = new BitmapImage();
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
string isoFilename = strImageName;
Stream stream = isoStore.OpenFile(isoFilename, System.IO.FileMode.Open);
image.SetSource(stream);
this.ImageBinary = image;
stream.Close();
}
[DataMember]
public string BookName { get; set; }
[DataMember]
public string BookPath { get; set; }
[DataMember]
public string FolderPath { get; set; }
}
这里我要添加列表数据类型的项目:
listobj.Add(new BookData(CoverImageForLib) { BookName = MyTitle.Value,
BookPath = item.Path, FolderPath = split[1] });
这里我将存储到隔离存储中的文件:
if (Settings1.FileExists("MyStoreItems"))
{
Settings1.DeleteFile("MyStoreItems");
}
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(BookDataList));
serializer.WriteObject(fileStream, listobj);
}
NavigationService.Navigate(new Uri("/Library.xaml", UriKind.Relative));
在这里,我只是存储值 . 我将从我的文件 MyStoreItems
反序列化 /Library.xaml
中的值
Edit:
我从here获得了相关的东西
您的“BookData”类没有公共无参数构造函数 . 尝试重新加载数据时,这将导致错误 . BitmapImage类不可序列化 . 为了解决这两个问题,您需要编写自定义序列化代码,您可以通过向您的类添加接口:“IXmlSerializable”并实现GetSchema,ReadXml和WriteXml方法来完成 .
序列化 listobj
时,我收到 System.Reflection.TargetInvocationException
异常 .
如果代码令人困惑,请向我提问,以便我澄清 .
Edit 1:
[DataContract]
public class BookData
{
[DataMember]
public string BookName { get; set; }
[DataMember]
public string Creator { get; set; }
[DataMember]
public string BookPath { get; set; }
[DataMember]
public string BookCoverPath { get; set; }
[DataMember]
public string FolderPath { get; set; }
[DataMember]
public BitmapImage Image { get; set; }
}
public class BookDataList : List<BookData>//for storing BookData class items with type of list
{
}
我将 BookCoverPath
序列化并存储为字符串 . 但每当我试图反序列化时,它都会给出异常 . 我不熟悉 DataContractSerializer
. 你能看一下我做错了吗?
// Here I getting data from file in Isolated storage
if (Settings1.FileExists("MyStoreItems"))
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(BookData));
listobj1 = (BookDataList)serializer.ReadObject(fileStream);
}
}
foreach (var item in listobj1)
{
string imagePath = item.BookCoverPath;
item.Image = BookData(imagePath);
}
ListBox.ItemsSource = listobj1;//binding isolated storage list data
public BitmapImage BookData(string imagePath)
{
//*** Image Binary ***'
BitmapImage image = new BitmapImage();
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
string isoFilename = imagePath;
Stream stream = isoStore.OpenFile(isoFilename, System.IO.FileMode.Open);
image.SetSource(stream);
stream.Close();
return image;
}
1 回答
BookData
类已实现加载BitmapImage
的逻辑 . 如果要在XML中持久化/序列化类,请删除此代码以获取simple DTO
. 这根本不需要自定义XML序列化程序 .要获取位图,请在反序列化xml后加载位图 . 你可以通过url加载图像,使用imageName来解析视图中的位图 . 或者创建BookData的Bitmap属性(但标记为
[IgnoreDataMember]
) . 反序列化元素后,在第二步中从isolatedStorage加载Bitmap,并在BookData上设置Bitmap .由于抽象和测试,我会建议你,在存储库类中实现
loadAndDeserializeFromXML()
和loadPicture(string path)
. 不是ViewModel / CodeBehind本身 .