There is an increasing trend for Microsoft platforms to build what is called a “Universal App”. In its ideal it is an application that you code once, and then it runs on desktop, tablet and phone platforms.
This opportunity introduces new challenges – with a main one being that the different platforms have differences. Differences in APIs, differences in capabilities and differences in display size and orientation.
All of these threaten the ideal of reusing most, if not all, of the code between the platforms. The less code you can share between the platforms, means:
· A higher cost for development – meaning either more resources needed, or more time required
· More code that needs to be maintained
· Higher probability of bugs, especially that impact one platform but another
The Model-View-ViewModel pattern has the potential to increase code reuse across the platforms. This can be done by minimizing logic in the View and designing a View Model that is agnostic of the details of the View. This will allow us to develop a different View for the different platforms, while sharing the code in the ViewModel and Model.
To illustrate this I’ve created a Windows Store application, called My Calc, which is purposeful coded in a naive View centric way. As is if one were to make the application available phone would be a large incremental cost.
MyCalc is a simple stack calculator application. One can push numbers on the stack. Operations, such as Add, take their operands from the stack and replace it with the resulting value. Purposefully the set of operations implemented as basic, as the object is to focus on ViewModel development, and not develop a fully featured calculator app.
In the various parts of this blog we’ll first refactor the application to have a ViewModel, and end with porting the application to phone by coding up a phone specific View.
Attached is our starting solution. It consists of 3 projects:
o Contains code shared across the platforms
o Contains code specific to Windows
o Contains code specific to phone
The shared project contains a StackCalculator class. This is our Model, or business logic. It doesn’t know anything about the UI.
You can ignore the WindowsPhone project for now, as all it has in it is the broiler plate code the Visual Studio put in when the project was created.
The Windows project has the MainWindow. If you’ve done UI development in XAML, WPF or Silverlight then likely you’ve seen (and written) code similar to this.
Here is a snippet of the xaml
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
There are a few things I’d like to point out about this code:
· The elements (buttons, ListBox, TextBox, etc…) have names so we can reference them in the code behind
· Events are wired up to methods in the code behind, such as the Clicked and TextChanged events.
These are indicators that this UI was coded up in a traditional way without a ViewModel (or at least not a well designed one). When we look at the code we get that confirmed with code like this:
privatevoid EnterBox_TextChanged(object sender, TextChangedEventArgs e)
if (EnterBox.Text.Length == 0)
EnterButton.IsEnabled = false;
BackspaceButton.IsEnabled = false;
EnterButton.IsEnabled = true;
BackspaceButton.IsEnabled = true;
Here we see that the logic in the View is very tightly coupled with the XAML markup, since the View not only responds to events from the controls, but directly updates the state of various controls.
While one could theoretically compile this code against 2 sets of markup (and there are some challenges to just making that happen) we are quite limited in the changes that could be made – other than just layout and style changes. For example the View code expects both an Enter and Backspace button – but what if, due to space constraints, on phone we would rather just use the ones on the soft keyboard rather than have such buttons in our main UI.