Category Archives: WinRT

WinRT only – AKA Modern UI sandboxed apps (formerly known as Metro)

Did ya hear the one about the serial killer?

Turns out his victim was found floating in a tub full of corn flakes and milk. Bahhh, dahhh, dahhh, da.

Anyway, in this case it was more that the serializer got killed…by an uncoöperative class. (BTW, that’s not an umlaut over the second “o”. It’s a diaeresis. Lovely sounding term. More on the subject: Typically, this happens because the class has a constructor with parameters. I couldn’t get around that. Well, I don’t want to anyway. I also didn’t want to depart from my vanilla JSON serializer, although in the past I’ve found SilverlightSerializer to be a solution when all else fails…like serializing an entire model. (Why would you want to do that? Long story, for some other time).

The solution is to simply mark up the class with the DataContract attribute and use DataMember for each of the members you want to serialize. In my case, this had the advantage of not serializing a voluminous sub-list I don’t want included. And it allows this class to be used in several places, rather than creating a separate Dictionary or similar.

public class Exercise
 public List<Asset> AssetsList;

public Exercise(int id, int order, string description)
 Id = id;

Order = order;

Description = description;

AssetsList = new List<Asset>();

 public int Id { get; set; }

 public int Order { get; set; }

 public string Description { get; set; }

And, voila, we end up with something that can be saved to IsolatedStorage:

[{“Description”:”JUMPING JACKS”,”Id”:0,”Order”:0},{“Description”:”WALL SIT”,”Id”:1,”Order”:1},{“Description”:”PUSH UPS”,”Id”:2,”Order”:2},{“Description”:”CRUNCHES”,”Id”:3,”Order”:3},{“Description”:”STEP UPS”,”Id”:4,”Order”:4},{“Description”:”SQUATS”,”Id”:5,”Order”:5},{“Description”:”TRICEPS DIP”,”Id”:6,”Order”:6},{“Description”:”PLANK”,”Id”:7,”Order”:7},{“Description”:”HIGH KNEES”,”Id”:8,”Order”:8},{“Description”:”LUNGES”,”Id”:9,”Order”:9},{“Description”:”PUSH UP & ROTATE”,”Id”:10,”Order”:10},{“Description”:”SIDE PLANK”,”Id”:11,”Order”:11}]

Kinda like Regex

So I’ve got this auto-incrementing C# TimeSpan that I need to format to just show single minutes and seconds, e.g. 3:48.

var timeSpan = new TimeSpan(0, 0, 0, secondsElapsed++);

Bugger took a while to figure out because I kept trying things like “m:ss” in my string formatter, not catching on that, just as in a Regex expression, I need to escape the colon. That can be done either by:

var timeStr = String.Format("{0:m\:ss}", timeSpan);


var timeStr1 = String.Format(@"{0:m:ss}", timeSpan);

I find that things like this take longer to figure out not because I don’t know how to do them, but because I have these preconceived notions of how they’re supposed to be done. Ordinarily, a string formatter is very forgiving of input so I expected it to “just work” in a certain way.

On another note, displaying a timer looks much better if you use a mono-spaced font. Otherwise, the number jump around as they change. Since this is being used in a Windows Phone app, I set the text block like this:

<TextBlock Name="TBlockTimerDisplay" TextAlignment="Right" FontSize="36" FontFamily="Segoe UI Mono"/>

THere are a couple other typefaces, such as Courier New, included with Windows Phone, but Segoe UI Mono would be my first choice, since it matches the default typeface for the OS.

WinRT Raw Notifications and Suspended State

Something to note if your WinRT app uses raw notifications. They will be received by the device but not by the app if it is suspended. This makes sense but may not be obvious sometimes because swiping the left side of the screen may still show the app on the stack. So it will appear to still be active, even if not in the foreground. The exception, of course, is if your app is registered to use a background service and the user has decided your app is important enough to be one of the “Lucky Seven” (those apps given permission to use the Lock Screen). I wouldn’t count on it.

It took me a bit to spot this behavior. I was noticing an instance of the app on one machine would sometimes receive raw notifications if it wasn’t in the foreground, sometimes not. The “not” occurred after a decent time lapse, which was the first clue. My server notification logs were no help because only client devices that were off or logged out of would return a status other than OK from the notification service. As mentioned, if the device is on and logged into, the notification service will successfully send the message. This makes sense. All it cares about is reaching the client at the specified URI. It’s up to the client to do what it wants with the received message. In this case, it appears the WinRT designers elected not to wake up an inactive app. Not a problem, but something to be aware of.

In my case, raw notifications are used to tell the app to update its data. It just means that the same sort of behavior also has to be triggered by resuming from a suspended state. BTW, for those who don’t know, you can suspend an app from within Visual Studio while debugging. To enable this, select TOOLS > Customize from the Visual Studio menu. Check the box next to “Debug Location”. If you have a solution with multiple projects you may have to select the right WinRT project from the Process pull-down menu.

Switching Between Remote Machines when Debugging

I’m currently switching between debugging on an ARM-based Surface RT and another remote machine (actually a VM running Windows 8 on my dev box). I wish there was a more convenient way to switch between remote machines, i.e. from the pulldown menu in Visual Studio. But at least there is a way. Open the project’s properties and select the Debug tab. You can then choose the remote machine. Using the Find button will show all the visible machines:


Surface – Initial Impressions

I’m typing this while sitting in SeaTac waiting for my flight back to Helena…on what else but the Surface? This is the 32 Gb version with the Touch Cover given out to developers attending the 2012 BUILD conference in Redmond. Typing on the Touch Cover isn’t as nice as a full regular desktop keyboard. Yet it’s passable, much better than onscreen typing and almost as good as most of the laptops I’ve ever used. That you can easily (and comfortably) rest it on your lap (using the built-in kickstand) is nothing short of fantastic. Keyboard shortcuts, such as CTRL-C, also work – overcoming the chief limitation of Surface, which is the Desktop interface. (More on that below).

The hardware is top-notch. I find the screen just as pleasant to view as the Retina display on the iPad 3. I’m picky about displays too. The keyboard just snaps into place (every time). The kickstand and overall build quality are solid but not heavy. Battery life is really good, although I haven’t done much video playback yet. Little things like the tiny white light on the end of the charger cable are nice touches. Also, having a USB port allowed me to keep my phone charged through long days of sessions and bus travel between Redmond and Fremont in NW Seattle.

The OS is generally good – more so in the “Metro” side. Some apps are a bit rough and a couple crashed on occasion. Yet it took no time at all to get the hang of the gestures that control the UI. The Desktop side is another story. Elements, such as the close button, are too small for touch. Changing a file name almost requires a prehensile tail using touch. The Desktop interface is clearly meant to be used with a keyboard and mouse. And that’s not as bad as it sounds. I see very few iPads without a cover or keyboard and none of those are as good as this one. One of those flexy-sexy Microsoft touch mice is next on my list to buy. I might even get a pressure-sensitive stylus.

Reading Kindle books is a bit odd at first because I prefer portrait mode. The aspect ratio of the Surface makes it feel like you’re reading a brochure. But it is easier to hold than the iPad. I wouldn’t say that the Surface is for everyone. But anyone using Windows will be able to get a lot out of it. And while the competition is hardly going to stand still I can tell you from what I saw and heard at BUILD this OS and ecosystem will only get better…and in some big, impressive and useful ways. Microsoft is hitting it hard on mobile services in particular. The development tools keep getting better too.

Dual-boot Win8/Win7 Environment

My 256 Gb (nominal) Samsung SSD arrived yesterday. It didn’t take long to install. But it did take a while to get Windows 8 RTM and my development environment set up. In short, the culprit turned out to be USB 3.0 on my Gigabyte motherboard. Once it was disabled, everything worked fine.

Prior to the install, my current setup was Windows 7 running on a 500Gb RAID1. I was a little nervous about impacting this, since it’s my bread-and-butter workstation. But changing over to either AHCI or RAID/IDE had no adverse affects. The reason I needed to get Windows 8 running on bare metal instead of inside a VM was so I could use Hyper-V. Without it, the Windows Phone 8 emulator can’t run.

Booting up from my Windows 8 installer DVD went fine and installation was basically without problems. I was pleasantly surprised to see that one of my two monitors displayed the classic Desktop and the other the Start Screen. This is perfect for development. I dutifully installed Visual Studio 2012 and then the Windows Phone 8 SDK. After that, I enabled Hyper-V and was prompted to restart.

That’s when the machine hung on the loverly new Windows 8 logo. I did a hard restart and Windows 8 ran a diagnostic (quicker than any version of Windows ever has) and prompted me to revert to the last working version of the system. That got me back into Windows. However, I lost my installs of Visual Studio and the SDK. I got them started on re-installing.

Meanwhile, I did some research into the issue with Hyper-V and discovered that with certain Gigabyte motherboards (mine is the GA-P55-USB3) the USB 3.0 driver is the problem. I went into BIOS after enabling Hyper-V again and disabled USB 3.0. I really don’t need it and won’t miss it. After that, everything is working, including the Windows Phone 8 emulator. I’m going to need to boot into Windows 8 to develop for the Windows Store and Windows Phone 8 for a couple months. But that’s fine since I tend to set aside dedicated blocks of time for that. I expect in a couple months I’ll find some time to migrate my current Win7 system over to Windows 8 (after creating either a Ghost backup or a disk2vhd image).