Friday, June 21, 2013

N=28 - Custom Bindings - N+1 Days of MvvmCross

Today, I thought I'd try something Deeply Technical

Custom Bindings

Actually it's not that technical... but it's a topic that people might feel a bit nervous about.

There's no need to worry - custom bindings are actually pretty simple.

For most UI controls/widgets in Android and iOS, MvvmCross binding "just works"

It does this using Reflection.
  • Wherever Mvx sees a property - e.g. public int Count {get;set;} - then it knows it can do one-way binding to it using Reflection.
  • Further, when it sees that there is also an event with a name ~Changed - e.g. public event EventHandler CountChanged; - then it also guesses that the event can be used for TwoWay binding too.

This works brilliantly well for many (most!) controls.

Problems only arise when whoever has written the control hasn't followed this convention - e.g.
  • if they've just provided arbitrary get/set methods instead of a public C# property
  • or if they've decided to signal their changes using a delegate or watcher pattern instead of a C# event.

In these cases, you can work around the challenge by using a Custom Binding.

The code for today is in:

For an excellent index of all the N+1 videos, check out:

No comments:

Post a Comment