• Marc A. Brown

Today has been a profitable day in terms of building the new app and in picking up new knowledge. I was adding contact information, including hyperlink buttons. I wasn't sure how to make them work. I figured there had to be some magical way to make it happen; however, turns out that it's not magical at all. It's the way I would have eventually done it. The answer (or non-answer, maybe?) was simple enough. It works as advertised.

I have been working on another Metro-based Windows 8 app for the last week or two, having taken a break from the attendance tracking app to let some ideas about it bounce around in the back of my mind. The new app is nearly finished and I wanted to take a look at including ads in it. The idea is that I can provide an ad-supported version of the app and then, if there is demand for it, provide a paid version that permanently removes the ads.

I signed up for an account at Microsoft's pubCenter and downloaded the ad SDK for Windows 8. I followed the instructions to set up an ad unit for my app, then added the ad control and set the application and ad unit id's appropriately. It's really quite simple to set up. Problem is, when I ran the app, I didn't get an ad. I discovered that the control provides an ErrorOccurred event (rather than throwing an exception), so I listened for that event and found that it was reporting the lack of the internet client capability. I knew that this capability had been set for the app (it's turned on by default in all new Metro app projects), so the error made no sense.

It wasn't until I ran the project through the Windows App Cert Kit that I made any progress. My app failed the certification test because it was built for debugging, which is a no-no for apps in the store. I rebuilt for release and... ADS! I now know that the control works and it actually makes sense that it wouldn't show any ads in a debug build. I only wish the error message had provided better information.

I have a bit more work to do on this new app before I'm willing to call it "done." The app functions well, but I would like to make some UI tweaks to provide more eye appeal. Not that it's bad looking now, I just feel that it could stand a bit of polish. Since I can't sign up with the Windows Store right now to submit an app (that process is currently down in preparation for the upcoming Windows 8 Release Preview … uh... release and the update to Visual Studio 11), I've got plenty of time to figure out what I need to clean up.

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