Windows Phone 7 MVVM ViewModel ideas

While writing a Windows Phone 7 application I started out NOT using MVVM. The prototype I started out with to get a feel for the required screens and navigation did not contain much logic and relied on data from a web service. At the time it felt like overkill to use MVVM. After trying to add more functionality and implementing the Marketplace requirements MVVM made a lot more sense.

A very basic implementation and using reflection to have a rename-safe implementation of INotifyPropertyChanged.

I also added a property named Application that allows fast access to the App object because the App object contains cache web service data and nearly all ViewModels use it.

When I was looking for a developer friendly way of implementing globalization and localization because I didn’t like the too complex bindings in the MSDN. I stumbled on this post and adapted it a bit for the next version of my ViewModel base class:

Easy testing of the localized texts by calling the ChangeLanguage method. By default the resources follow the Phone’s UI language.

On the other hand it has drawbacks as well:

Because, opposed to the MSDN way, I am using the DataContext it is no longer possible to select a text using the StaticResource reference dialog unless I setup a design time DataContext

For the same reason the designer in Visual Studio does not show the text so the designer becomes pretty useless too.

These are bad but I’ll try to fix them and will write a post on that when I find a solution.

Next up: error messages. Silverlight applications usually handle logic asynchronously. This means that you’ll need a way of passing errors back to the UI thread and you’ll need to find a way of displaying them. I thought about this and figured that MessageBoxes wouldn’t do for me. When processing logic asynchronously and popping up a message box it probably will not be clear to the user where the error originated and the flow of the application will be disturbed too.

The ShowError method is the method that a ViewModel would use to set an error. The View that is responsible for showing the message can simply bind to the properties ErrorMessage and ErrorVisibility like this:

The application heavily relies on a live connection to the web so notifying the user that the connection has been lost is needed in many places and again without hampering the user with modal boxes and breaking application flow. So I created a special sub class of the ViewModel base class that is connection aware:

The property IsDownloading should be set by a ViewModel when it starts a request across the web. The Marketplace requires an application to be responsive and never have the user wait for more than 5 seconds. Because a download depends on network speed and available bandwidth a View can bind to DownloadVisibility and show a progress bar or give another visual clue. Similarly the View can bind to DownloadProgress to give the user more information on the download. the ViewModel will have to set the DownloadProgress propery.

The view can bind to the DisconnectedVisibility property to indicate that the application has connection issues.

These ViewModel base classes are still work in progress and they have only proved themselves in a single application.