发生类型的未处理的异常'System.AccessViolationException“ [英] An unhandled exception of type 'System.AccessViolationException' occurred

查看:157
本文介绍了发生类型的未处理的异常'System.AccessViolationException“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

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

和最终反序列化到一个列表框控件作为这样一个列表:

And finally deserialize into a List for a ListBox control as such:

       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:

I keep getting a System.AccessViolationException on the line of code:

 recipe.ListPreview.SetSource(stream);



基本上我想在这里做的是允许用户定义的图像绑定到一个列表框。因为你不能序列化的BitmapImage,我反而将文件保存到IsolatedStorage和路径保存到一个名为的ImagePath字符串。当我反序列化创建列表框我一个名单,我把图像路径,打开图像文件,将其设置为BitmapImage的那个,然后绑定到一个列表框的来源。在我的代码,一切工作正常,除了代码,系列化,deserialzation双方的工作,一条线完美和有约束力的图像文件直接Image控件从IsolatedStorage完美的作品也是如此。

Basically what I am trying to do here is allow a user defined Image to bind to a ListBox. Because you can't serialize a BitmapImage, I instead save the file into IsolatedStorage and save the path into a string called ImagePath. When I deserialize to create a List for my ListBox, I take the image path and open the image file and set it to the source of a BitmapImage that then binds to a ListBox. Everything in my code works fine except that one line of code, serialization and deserialzation both work flawlessly and binding the Image file to an Image control directly from IsolatedStorage works perfectly as well.

你认为可能会导致AccessViolationException?

What do you think may be causing the AccessViolationException?

在此先感谢!

推荐答案

虽然这是不完全一个解决方案,我发现了一个方法来得到这个工作。我定义了一个新的BitmapImage,设置它的源从IsolatedStorage的图像,然后我设置的BitmapImage等于recipe.ListPreview。

Though this isn't exactly a solution, I found a way to get this to work. I define a new BitmapImage, set it's source to the Image from IsolatedStorage and then I set that BitmapImage equal to recipe.ListPreview.

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



感谢您的帮助!

Thanks for your help!

这篇关于发生类型的未处理的异常'System.AccessViolationException“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆