• Marc A. Brown

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

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

0 views0 comments

Recent Posts

See All

Well, here I sit with a second Microsoft app store certification rejection. I'm trying to get a critical fix for my Digital Blasphemy browser app published but Microsoft keeps shooting me down. They h