I've decided to dabble a bit in MonoDroid and MonoTouch and port one of my WP7 apps as a starter. I would really like to reuse my existing ViewModels but since both Android and iOS seem to have no such thing as XAML's strong databinding I would like to ask if anyone went that route before and can recommend some best practices or existing solutions.
2 Answers
We're doing this with an application right now, but writing for iOS first (even before Windows). It is not full of rainbows and ponies for sure.
I would recommend the following:
- Use a MVVM framework on Windows that doesn't require you to expose ICommand on every action the user takes (like Caliburn, for example), it also shouldn't require a dependency to it from within all of your ViewModels.
- Conditionally inherit the WPF-specific pieces of your ViewModelBase class, you can do this with partial classes or an
#if iPhone
directive. INotifyPropertyChanged or ICommand are examples. - Use an IoC container, it is very helpful to abstract out things like saving settings to the filesystem which will be very different on all platforms. Also helps to sort out your dependencies as well, which is very helpful for separating out platform-specific code from non-platform specific.
- Use a "messenger" of some kind (example here), usually included with an MVVM framework. This is a must in my opinion, at least for iOS. Apple's MVC is so all over the place, it's better to have global messages you can subscribe to in a weak referenced (and decoupled) way.
- Use MVC on each platform like you would natively, then treat each ViewModel as you would if you were calling it manually. There are no UI bindings, no ICommand, so keep your ViewModel's simple.
- Linking files is the best trick ever. You don't want a copy of each view model per platform, so make sure you know how to link to a file within projects in Visual Studio and MonoDevelop. This also makes
#if iPhone
and#if Android
statements possible.
I know you are working with an existing application, so it's tough. It may be simpler to just reuse your business model and that's it. Android and iOS have MVC patterns of their own, and are drastically different from WPF. You might also only need a subset of each ViewModel on mobile devices, which could make it easier to just rewrite.
In our case:
- We're using TinyIoC (also has it's own messenger)
- We will use Caliburn-Micro when we start on WPF, we don't need some of the features in full Caliburn

- 26,115
- 21
- 99
- 182
-
Thanks for your insightful answer Jonathan. One question. Did you at some point try to leverage INotifyPropertyChanged by subscribing to PropertyChanged from within your view and kind of emulate one-way binding? – Oliver Weichhold Nov 15 '11 at 13:48
-
I used the messenger from TinyIoC to update the View. When you get into iOS, you'll see why. You have to subclass sometimes 3 or more classes to get a complicated View working and it's a pain to have to pass your ViewModel through to all those other objects and hook up an event. The messenger is just a nice, decoupled way to get the same thing done. – jonathanpeppers Nov 15 '11 at 14:00
-
I assume with messenger you mean something similar or equal to the EventAggregator pattern which I alreay use extensively in my projects for losely coupled navigation via a central controller and events concerning multiple VMs. – Oliver Weichhold Nov 15 '11 at 14:28
-
Yes, exactly. There are other names for it. It is IEventAggregator in Caliburn, but we used TinyIoC's TinyMessenger b/c we needed the functionality on the iPhone without a reference to Caliburn. – jonathanpeppers Nov 15 '11 at 16:48
-
Your statement about linking to source files in projects (instead of adding them) is invaluable. I have a .NET/Mono astronomy library that shares all of its C# source between two sets of solutions and projects; one set for Visual Studio and another for MonoDevelop. Sound advice! Cheers! – NovaJoe Feb 07 '12 at 21:30
I've recently finished a large project which we wrote wp7 first, and which was then ported into touch and droid.
As part of this we've released our own mvvm framework - including some databinding support for touch and droid - the source is available at http://github.com/slodge/mvvmcross
The experience of porting to droid was good - the axml layout files provided a good hook for databinding. Currently, however, i'm not quite as happy with the binding we achieved for touch - although montouch.dialog does at least provide us with some nice looking code sometimes.

- 66,722
- 7
- 114
- 165