
Written by Filip Dunđer, Software Engineer at HTEC
Getting to know MvvmCross
Using Xamarin as a cross-platform mobile development technology has been happening for a while at HTEC. Our mobile devs are digging deep and trying out new tools and approaches, so this time, we have decided to present components of the MvvmCross framework specifically designed for Xamarin. This framework is based on the Model-View-ViewModel (MVVM) design pattern and supports Xamarin.iOS, Xamarin.Android, Xamarin.Mac, Xamarin.Forms, UWP and WPF. When creating modern UIs using MvvmCross you may encounter several major obstacles due to an insufficient amount of tutorials and instructions on official pages and forums. While creating Xamarin Native projects using the MvvmCross framework, you’ll notice that not all properties are available for binding, or even that two-way bindings don’t come out of the box. This is when you need to use custom data bindings. In the following tutorial, we will show how to create a custom binding for a property that doesn’t have binding support out of the box. This will be covered in the Android example. Moreover, we will talk about two-way bindings which will be covered in a more complex iOS example.Target Bindings
Custom binding (Android)
Target bindings are used when we want to create an additional property that is not part of the model/object to which we bind or when we want to create a two-way binding. MvvmCross supports a lot of binding properties on multiple platforms, but usually that’s not enough. For example, if we want to change tint color for an image view on Android we need to create a custom binding because binding for tint color on image view doesn’t exist for the Android platform. MvvmCross allows us to create custom binding by inheriting a base class called MvxTargetBinding. There are additional target binding classes that we will cover later. Let’s cover a basic example for Android tint color. In order to make this work we need to do the following things:- Create a target binding class that will change the tint color
- Register that class and custom property name
- Do binding

Two-way binding (iOS)
Since we’ve covered the example for Android, let’s write one for the iOS platform, but let’s make things a bit more complex. Sometimes requirement changes happen mid-project. In my case, I had to add an additional button in the iOS application that would reset the color of an image to default. Since the Android didn’t need to have this and we needed to support this on iOS we did not change the core project which has common logic. The following example will cover this situation. This means that we will have to create a two-way binding that will update the view model property on button click. In order to do this we will use MvxWithEventPropertyInfoTargetBinding. Two-way bindings require an event that will be fired when a specific property changes. Since we will be using UIImageView for control which will display logo image, we will need to extend it and add an event that will be raised when the TintColor property changes. Let’s extend UIImageView to add event support. (view code) Keep in mind that we are using event without arguments since Mvx binding will not detect the type if it’s not “EventHandler”. The next step is to create a binding class. Same as in Android we will create a target binding class, but in this scenario, it will support two-way bindings. (view code) The next step is to register it. Now we will do the same thing as in our Android project, but this time we will do it in an iOS setup class. Again, we override FillTargetFactories and register our custom binding. (view code) Now we need to do binding, which is quite different from the one in Android. Since iOS doesn’t support bindings through the designer, we will do it in the code behind of a view. The code behind the view should look as follows. (view code) ResetColorToDefaultButton is the name of the additional button that we added to the iOS view. You can notice that we are using a different converter for the iOS project and the reason for this is that the default color converter doesn’t support conversions in both directions. The app will look similar to the Android version, except it has an additional button to reset the color to the default one.