0

am developing an app for Windows Phone 8.0 in C#

I need to save a class which contains a List of Images : List<Image> as a property by using IsolatedStorageSettings

First time , the app crashed at saving code line with System.Runtime.Serialization.InvalidDataContractException

I followed the instruction for Serializing an object but still i still get the same exception

Code:

Lists Class

 [DataContract]
    public class Lists
    {
        [DataMember]
        public List<Image> ImageTiles = new List<Image>();
    }

Saving :

        ImageTiles.Add(CroppedImage);
        lists = (Lists)levels["Lists"];
        lists.ImageTiles = ImageTiles;
        levels["Lists"] = lists;
        levels.Save();

What is still missing ?

GabourX
  • 283
  • 2
  • 5
  • 14

1 Answers1

0

You are getting that error because Image is an invalid type for serialization. The file would actually need to be sent to a stream and written out to a file. Here is a website with some tutorials on the subject:

http://code.tutsplus.com/tutorials/working-with-isolated-storage-on-windows-phone-8--cms-22778

A particularly helpful block of code from the article is below:

private void saveGameToIsolatedStorage(string message)
{
  using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
     using (IsolatedStorageFileStream rawStream = isf.CreateFile("MyFile.store"))
    {
      StreamWriter writer = new StreamWriter(rawStream);
      writer.WriteLine(message); // save the message
      writer.Close();
    }
  }
}

More on I/O here:

https://msdn.microsoft.com/en-us/library/system.io(v=vs.110).aspx

And how to get a stream from an image here:

Save stream as image

So in overview, here is what you need to do:

1) Send your image to a stream

MemoryStream ms = new MemoryStream();
myImage.Save(ms, ImageFormat.Jpeg);

2) Use IsolatedStorageFile to save to stream

  using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isf))
    {
        fileStream.Write(ms.ToArray(), 0, ms.Length);
        fileStream.Close();
    }
}
Community
  • 1
  • 1
Matthew Frontino
  • 496
  • 2
  • 12