Xamarin.Forms Programming in F#

Things are kind of busy what with Evolve being only 40 days away and iOS 8 coming down the pipe, but I thought I'd share the easy hack that allows you to program Xamarin.Forms with F#.

(Of course, Xamarin loves F# and official support and templates and documentation and all that sort of stuff is forthcoming. This is just something you can do for the moment to begin exploring Xamarin.Forms with F#.)

tl;dr: Use the beta PCL 78 F# Core and link to the facade assemblies for monotouch

OK, so assuming that was a bit too brief...

In Xamarin.Studio create a "New solution..." of type F#/iOS/iPhone/Empty Project...

Open the "References" folder and delete the existing reference to Fsharp.core.dll.

Right-click the solution and select "Add Packages..."

Screenshot 2014-08-27 15.42.39

In the NuGet dialog, select "Show pre-release packages" and type FSharp.Core into the search box. This should allow you to add the "FSharp.Core Mono delay signed" package.

Screenshot 2014-08-27 15.25.05Also, add the Xamarin.Forms package:

Screenshot 2014-08-27 15.46.26

And now the tricky part!  You have to add references to the System.ObjectModel.dll and System.Runtime.dlls from the monotouch facade assemblies by hand.

Right-click on the References folder, Select "Edit...", and select ".NET Assembly". Add references to System.ObjectModel.dll and System.Runtime.dll from, in my case:

/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/Facades/

Your path may be a little different.

Write a Xamarin.Forms app in 36 lines of code :

[code lang="csharp"]

namespace FSXF1

open System
open MonoTouch.UIKit
open MonoTouch.Foundation
open Xamarin.Forms

type App = class
static member GetMainPage =
let lbl = new Label()
lbl.Text \<- "Hello, F# Xam.Forms!"
lbl.VerticalOptions \<- LayoutOptions.CenterAndExpand
lbl.HorizontalOptions \<- LayoutOptions.CenterAndExpand

let cp = new ContentPage()
cp.Content \<- lbl
cp
end

[\<Register("AppDelegate")>]
type AppDelegate() =
inherit UIApplicationDelegate()

member val Window = null with get, set

// This method is invoked when the application is ready to run.
override this.FinishedLaunching(app, options) =
this.Window \<- new UIWindow(UIScreen.MainScreen.Bounds)
Forms.Init()
this.Window.RootViewController \<- App.GetMainPage.CreateViewController()
this.Window.MakeKeyAndVisible()
true

module Main =
[\<EntryPoint>]
let main args =
UIApplication.Main(args, null, "AppDelegate")
0
[/code]

And you're good to go!

Screenshot 2014-08-25 10.16.14

P.S. If it helps: https://github.com/lobrien/HelloXamarinFormsFSharp