Porting MVVMCross to Xamarin.Forms

MVVMCross and Xamarin Forms framework both follow MVVM design pattern. The difference between two frameworks is that in MVVMCross, the binding with the framework is done using base classes where as in Xamarin forms, the binding is done through interfaces. The other difference is in MVVMCross, the binding between View and ViewModel is done by convention and reflection by default. In Xamarin forms, the binding can be done declaratively or programmatically.

Porting MVVMCross application to Xamarin forms requires changes in two layers ViewModel and View.

ViewModel
In MVVMCross, View Models integrate with the framework by inheriting from MvxViewModel class. In Xamarin forms, they integrate by implementing INotifyPropertyChanged interface.

MvxViewModel class handles the functionality of notifying property change. In Xamarin forms, there is no equivalent class. But view models should implement IPropertyNotifyChange interface. To preserve investment in the existing view models, it is a good practice to create a base ViewModel class that implements the interface. And inherit existing view models from it.

In MVVMCross, commands are inherited from MvxCommand. In Xamarin forms, they are represented by ICommand interface. Xamarin forms also provides Command class that implements the interface. Porting would be simply replacing MvxCommand with Command class.

View
In MVVMCross, the views are written separately for each platform. If the app is written for iOS/Android platforms, there is no XAML present in those platforms. In that case View needs to be built from scratch.

If a view is written for Windows Phone application using XAML, some part of it (such as Grid) can be reused. However, Microsoft XAML is not 100% compatible with Xamarin forms. It would probably require substantial changes.

One XAML doesn’t fit all platforms. Xamarin forms provides ability to customize resources per platform in an elegant way. To keep XAML clean, it would be a good practice to keep the platform specific customization of properties in the resource dictionary and bind properties to the static resources e.g. fontHeight.

Regarding controls available in Xamarin Forms, as of Oct 2015, it did not have HyperLinkLabel control. The HyperlinkLabel control was picked up from open source https://github.com/XLabs/Xamarin-Forms-Labs/wiki/HyperLinkLabel and fixed/changed as required. Missing controls can be taken from open source or developed using custom renderers.

Some controls do not have full blown MVVM functionality. Picker has SelectedIndex BindableProperty, but it does not have Items as BindableProperty. Xamarin forms does have a decent set of controls, but that may not be sufficient.

Example code https://github.com/sameerkapps/smart-switch
MVVMCross – PowerUpApp
Xamarin Forms equivalent – SmartSwitch.Forms

Advantages of Xamarin Forms:

  • By having common UX layer, it increases amount of shared code for cross platform development.
  • It is easy to apply platform specific attributes.
  • With interface binding, it provides loose coupling.

Disadvantages of Xamarin Forms:

  • If UX is highly platform specific, this is not recommended
  • Xamarin Forms do not have visual designer. The XAML needs to be hand coded.
  • XAML errors are thrown at runtime and not caught at compile time.
  • There is a shortage of controls provided by forms. May need to use third party libraries.
Advertisements

One thought on “Porting MVVMCross to Xamarin.Forms”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s