© Marc A. Brown. All rights reserved.

  • White Facebook Icon
  • White Instagram Icon
  • White Twitter Icon

Developing for Metro, Part 4 -- Data Storage and Retrieval

April 17, 2012

Thanks to some sample code from Microsoft, storing and retrieving data turned out to be a breeze. I used their SuspensionManager class (from a suspend/resume sample project) as a model for storing and retrieving my data to an XML file in the user's application data folder. I, of course, had some trials while getting it to work properly. I was unable to serialize the images in my app, so I had to make a change to my data classes to serialize the URI to the image instead. I also ran into a bit of trouble because constructors are not executed when deserializing an object. I had to revisit images again to ensure that they would get loaded. I was quite pleased at how easy it was to do. I added code to the Image property getter to set it up if it hasn't been already. However, I went back and removed this code when I discovered the information below.

While working on adding the attendance page, I discovered that I needed to make a change to my data classes to get internal property change listeners set up after deserializing objects. Those listeners would normally be set up in the constructor, but the constructor doesn't run for objects that are created as part of the deserialization process. Fortunately, there's an easy way to fix this.

[OnDeserialized]
private void DeserializingCleanup(StreamingContext c)
{
  PropertyChanged += Attendance_PropertyChanged;
}

The [OnDeserialized] annotation indicates that the method should be run after an object is deserialized and the StreamingContext parameter is required in conjunction with that annotation. For the data class that includes images, I added a second line to this method to load the image.

Since I began work on this article, I was forced to make some additional changes to my data model -- all part of the joys of learning my way around a new system. I now store images for objects in a directory created and set aside specifically for those images and the images are named using the ID of the object they belong with, eliminating the need to serialize anything directly related to the image. However, the work I did to make images work the previous way was valuable because I learned about the serialization annotations like the one I discussed above.

See also:

Developing for Metro, Part 1

Developing for Metro, Part 2

Developing for Metro, Part 3

Share on Facebook
Share on Twitter
Please reload

Recent Posts

June 17, 2017

Please reload

  • White Facebook Icon
  • White Twitter Icon
  • White Instagram Icon
Follow Us
RSS Feed