【问题标题】:An unhandled exception of type 'System.AccessViolationException' occurred发生“System.AccessViolationException”类型的未处理异常
【发布时间】:2013-08-28 08:02:08
【问题描述】:

我有以下课程:

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

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

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

提前致谢!

【问题讨论】:

  • What do you think may be causing the AccessViolationException? 该文件可能仍在被另一个进程使用(尽管它看起来被正确处理.. hmm)打开流时,尝试将文件共享设置为FileShare.ReadWrite
  • 很遗憾,这不起作用:/但您可能对另一个进程正在使用的文件是正确的,我会看看那个。
  • 所以这是我发现的,如果定义一个新的 BitmapImage 并且我分配它的源,它只会出现来自 RecipeItem 类的 BitmapImage。
  • 这表明该对象仍在被另一个进程使用

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


【解决方案1】:

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

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

感谢您的帮助!

【讨论】:

    【解决方案2】:

    不是 shure,但我认为您不能直接在 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())
      {...}
    }
    

    可能是另一个导致错误的原因

    【讨论】:

    • 哇,我不敢相信我没有注意到我定义了两次IsolatedStorageFile,谢谢!
    • 只是一个简单的问题,我将如何将文件从 IsolatedStorage 写入 MemoryStream,并且使用 IsolatedStorageFileStream 是一样的吗?
    猜你喜欢
    • 2016-08-28
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多