c# - 发生类型为 'System.AccessViolationException' 的未处理异常

标签 c# serialization windows-phone-8 listbox bitmapimage

我有以下类(class):

 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 控件的列表:

       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);

基本上我在这里尝试做的是允许用户定义的图像绑定(bind)到列表框。因为不能序列化 BitmapImage,所以我将文件保存到 IsolatedStorage 并将路径保存到名为 ImagePath 的字符串中。当我反序列化为我的 ListBox 创建一个列表时,我获取图像路径并打开图像文件并将其设置为 BitmapImage 的源,然后绑定(bind)到 ListBox。我的代码中的所有内容都工作正常,除了一行代码、序列化和反序列化都可以完美地工作,并且直接从 IsolatedStorage 将 Image 文件绑定(bind)到 Image 控件也可以完美地工作。

您认为可能导致 AccessViolationException 的原因是什么?

提前致谢!

最佳答案

虽然这不是一个确切的解决方案,但我找到了一种方法让它发挥作用。我定义了一个新的 BitmapImage,将其源设置为来自 IsolatedStorage 的图像,然后将该 BitmapImage 设置为等于 recipe.ListPreview。

BitmapImage Test = new BitmapImage();
Test.SetSource(stream);
recipe.ListPreview = Test;    

感谢您的帮助!

关于c# - 发生类型为 'System.AccessViolationException' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482381/

相关文章:

c# - ASP MVC 中的 Multi-Tenancy

java - 在 Java 中使用 JAXB 映射命名空间

c# - Button Click to get an Object on SelectionChanged 事件

c# - WPF 消息框本地化

c# - 无法加载文件或程序集 'WebGrease,版本 = 1.5.1.25624

c# - 为什么元组在 `[Serializable]` 时没有无参数构造函数?

java - 创建具有大量选项的位掩码

c++ - 以编程方式折叠 HubSection 崩溃 - WP8 C++

sql - WP8 SQlite选择查询

c# - 如何在 Windows Phone 8 中打开 .txt 和 .docx 以及 .xls 文档