I have the following 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; }

}

That I serialize as such:

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

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

 recipe.ListPreview.SetSource(stream);

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.

What do you think may be causing the AccessViolationException?

Thanks in advance!

有帮助吗?

解决方案

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!

其他提示

not shure but I think you can't directly use the IsoStorageStream in the UI. try to write into a MemoryStream and use that memoryStream for the 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;
     }
}

otherwise, I don't understand why you do this :

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
try
{
  using (store = IsolatedStorageFile.GetUserStoreForApplication())
  {...}
}

maybe another cause of bugs

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top