For my next exploration of the MVC design pattern, I wanted to convert my “Hubble Space Telescope” application from a desktop WPF app to Windows Phone 7. This can help demonstrate the flexibility MVC provides with such a scenario.
You can see the previous article here: http://www.hitcents.com/blog/post/developing-iphone-%E2%80%93-part-4-controlling-hubble-space-telescope-mvc
By using the MVC pattern, we only have to rewrite a few portions of the application to get it running on a Windows Phone. Luckily, our Model and Controller layers will remain completely untouched when porting from WPF on the desktop to WP7. My original port to the iPhone was much more of a hassle, since Apple’s framework is drastically different from WPF.
Let’s break down our changes for WP7:
What’s missing in WP7?
After setting up my projects for WP7, for the most part I could directly copy the XAML from WPF. Here are the missing pieces I had to account for:
Turns out Silverlight (version 3 and below) and WP7 both are missing much of the ICommand functionality that is so great in WPF. To alleviate this, I had to reference System.Windows.Interactivity and use a couple helper classes from a codeplex project here: http://expressionblend.codeplex.com
Here is a quick code example of making commands work in WP7:
So what’s left? A first compile of the app shows that we are missing some Dispatcher implementation I used in my Execute class. This was just a simple class to handle marshalling changes to the UI thread.
Here is my updated Execute class that works on WP7:
Note: you of course have to set the Dispatcher property somewhere; I did so in my MainView.
So far, it looks to me like Microsoft has done a great job in designing the SDK and development tools for Windows Phone 7. It only took me around 30 minutes to port this small app and work out the details, which would have been comparable to porting the application to Silverlight.
If you are interested in the source, checkout the latest at our codeplex project for our blog here.
Next time I'm going to look at using some MVVM frameworks on this application, such as Caliburn and possibly MVVM Light.