我有以下课程:
public class RecipeItem
{
public Guid ID { get; set; }
public string Title { get; set; }
public string Instructions { get; set; }
public string Ingredients { get; set; }
public string ImagePath {get; set;}
[XmlIgnore]
public BitmapImage ListPreview { get; set; }
}
我这样序列化:
private void SaveRecipe()
{
fileName = recipeID + ".txt";
recipe.Title = TitleBox.Text;
recipe.Ingredients = Ingredients.Text;
recipe.Instructions = Instructions.Text;
string tempJPEG = "image" + recipeID + ".jpg";
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
using (store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(tempJPEG))
{
recipe.ImagePath = tempJPEG;
}
using (var file = store.CreateFile(recipe.ID + ".txt"))
{
XmlSerializer serializer = new XmlSerializer(typeof(RecipeItem));
serializer.Serialize(file, recipe);
}
}
store.Dispose();
}
最后反序列化为ListBox控件的List,如下所示:
public static List<RecipeItem> CreateTestList()
{
List<RecipeItem> list = new List<RecipeItem>();
RecipeItem recipe = new RecipeItem();
//Get files from isolated store.
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(RecipeItem));
var filesName = store.GetFileNames();
if (filesName.Length > 0)
{
foreach (string fileName in filesName)
{
if (fileName == "__ApplicationSettings") continue;
using (var file = store.OpenFile(fileName, FileMode.Open))
{
try
{
recipe = (RecipeItem)serializer.Deserialize(file);
}
catch
{
}
}
using (store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (recipe.ImagePath!=null)
{
using (var stream = store.OpenFile(recipe.ImagePath, FileMode.Open, FileAccess.ReadWrite))
{
recipe.ListPreview.SetSource(stream);
recipe.ListPreview.DecodePixelHeight = 100;
recipe.ListPreview.DecodePixelWidth = 100;
}
}
}
}
}
}
catch
{
}
list.Add(recipe);
store.Dispose();
return list;
}
我在代码行上不断收到System.AccessViolationException:
recipe.ListPreview.SetSource(stream);
基本上我在这里尝试做的是允许用户定义的Image绑定到ListBox。因为您无法序列化BitmapImage,所以我将文件保存到IsolatedStorage中,并将路径保存到名为ImagePath的字符串中。当我反序列化为我的ListBox创建一个List时,我采用图像路径并打开图像文件并将其设置为BitmapImage的源,然后绑定到ListBox。我的代码中的所有内容都可以正常工作,除了一行代码,序列化和反序列化都可以完美地工作,并且可以直接从IsolatedStorage将Image文件绑定到Image控件。
您认为可能导致AccessViolationException的原因是什么?
提前致谢!
答案 0 :(得分:0)
不舒服,但我认为您无法在UI中直接使用IsoStorageStream。尝试写入MemoryStream 并将该memoryStream用于imageSource。
//something like this
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var storeStream = store.OpenFile("file.bin", System.IO.FileMode.Open))
{
var memoStream = new System.IO.MemoryStream();
storeStream.CopyTo(memoStream);
return memoStream;
}
}
否则,我不明白你为什么这样做:
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
try
{
using (store = IsolatedStorageFile.GetUserStoreForApplication())
{...}
}
可能是导致错误的另一个原因
答案 1 :(得分:0)
虽然这不是一个完全解决方案,但我找到了一种让它发挥作用的方法。我定义了一个新的BitmapImage,将它的源设置为来自IsolatedStorage的Image,然后我将BitmapImage设置为等于recipe.ListPreview。
BitmapImage Test = new BitmapImage();
Test.SetSource(stream);
recipe.ListPreview = Test;
感谢您的帮助!