I'm currently evaluation Xamarin as a replacement for a Phonegap application. The first thing I'm evaluating is building UIs in code. Since most of the example code is in C#, I initially built my test UI with it. In poking through the Xamarin documentation I came across the MonoTouch.Dialog toolkit (hereafter MT.D). MT.D is a declarative framework for creating iOS UIs. Here is the code for the MasterViewController of a UISplitView (this is a minor modification to the code found in the Xamarin documentation):
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.Dialog;
using System.Linq;
namespace CSharpTabbed
{
public partial class MasterViewController : DialogViewController {
Section testSection;
public MasterViewController () : base (null)
{
Root = new RootElement ("Items") {
new Section(){
new StringElement("Section 1"),
from num in Enumerable.Range(1,10)
select new ImageElement(new UIImage("first.png"))
},
new Section(){
new StringElement("Section 2"),
new Section(){
from num in Enumerable.Range(1,10)
select new StringElement("Item" + num})
}
},
new Section(){
new StringElement("Section 3"),
new Section(){
from num in Enumerable.Range(1,10)
select new StringElement("Item" + num)
}
}
};
}
public override bool ShouldAutorotateToInterfaceOrientation
(UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
}
And the resulting UI:
Since this requires the construction of a lot of collections I thought I would try it in F#. Here is my direct conversion from C# to F#:
This isn't really any better than the C# due to all the casting and the need to interface with Collections.Generics.List. It would also be nice if I could use any collection type I want. To address these issues, I created a library which allows more idiomatic F# usage of MT.D. Usage looks like this:
This gives compile time checking without having to use a lot of type annotations while still maintaining type safety.
In progress library here.