Thursday, January 31, 2013

Awesome MvvmCross Swiss Bindings for MonoDroid and MonoTouch

Recent changes in MvvmCross introduced Swiss Bindings for MonoDroid and MonoTouch. What is so great about them? Mainly readability, but it is also a lot more easy to write. Instead of having to write JSON you make bindings in a similar manner as it is done in XAML.

Let's dive straight into some samples to see how awesome it is!

Binding a TextView
So before you had to write all sorts of tags to bind stuff, this is not the case anymore. Comparing the two approaches below quickly show, how much less effort you have to put in making your bindings and the Swiss Bindings look a lot more clean. 16 less characters to write! Binding a Spinner
Now lets try something a bit more complicated, like binding a Spinner! See that! So much more clean and readable! Sweet mother of god, in this case we skip 35 characters! Swiss Bindings are really awesome, or at least that is what I think!

To try them out you do not really have to do anything. Just use them, they are already there to use in your projects and the binding parser is made so that it detects what kind of binding you are using depending on the first character.

Syntax
The syntax of Swiss Bindings is pretty simple and I have made a simple grammar and diagrams to visualise it:
Railroad Diagram (Click for Large)

The ChainedViewModelProperty can be a lot of things, so to simplicity I have left out all the posibilities and described it with a single parameter. But just like in XAML it can be a Path to the Property you want to display so: I hope you get the picture, modelling all the possibilities would have been a huge job to do. Also I know that the BNF grammar is sort of wrong, but it gets the job done for me describing how this Swiss Bindings work. If you need help using it, join us at JabbR.Net in the #mvvmcross chat.

3 comments:

  1. While we're saving characters... one thing I've wondered is...

    Why does all Android code seem to use 'android:' as the attribute namespace - wouldn't 'a:' be much easier to read and faster to type too?

    ReplyDelete
    Replies
    1. You are right, however you are free to change that I think? In each layout you define the namespace as "xmlns:android=..." so you could just change that to whatever you like. Microsoft does that with their XAML, where they have a lot of brief namespace names.

      Delete
  2. Slodge your bindings works awesome by the way! I love it.

    ReplyDelete