The other evening I went on the hunt for a XAML/C#-based example of a game loop. I ran into a couple of OLD blog posts by Mike Snow. Naturally it caught my eye, simply because of his last name.
In his posts he described game loop based on Silverlight 2. OK. Silverlight uses XAML, and the code was C#. XAML/C# are first-class citizens in the Windows 8 Metro style app world, so I figured, why not see what it takes to make this work on Windows 8?
The posts I used as reference:
The first post contains the code for the majority of the application – the XAML and the C# code. In second post Mike switches the game loop to use CompositionTarget.Rendering instead of DispatcherTimer or the Storyboard timer. Are they still around in the Metro XAML world?
To find out, I open Visual Studio and took a look in the Object Browser. Guess what I found.
Yep. DispatcherTimer, Storyboard, and CompositionTarget are all alive and well in the Windows.UI.XAML namespaces.
So I figured, why not give it a try?
It turned out that most of the “work” was simple copy & paste. I had to change the using statements to point to the Windows.UI.Xaml namespaces for the controls. Had to change the signature for a couple of the event handlers. And a few other minor things.
Here are the steps I took to make the port.
Time to run the app. You should see this
The running app.Congratulations.
It’s very easy to change the game loop to use the CompositionTarget.Rendering event instead of a Storyboard.
And that’s it.
As you’ve probably heard over and over again, if you’re a Silverlight developer you have the skills to be a Windows 8 Metro style developer.
A great resource for developing Metro style apps on Windows 8 is http://dev.windows.com.
Happy Windows 8 developing!
--bliz (@snowstormlife)