--- author: "Jim Bennett" categories: ["xamarin", "xamarin.forms", "technology", "f#", "fsharp", "elmish"] date: 2018-05-06T15:00:40Z description: "" draft: false slug: "building-mobile-apps-in-f-using-xamarin-forms-and-elmish" tags: ["xamarin", "xamarin.forms", "technology", "f#", "fsharp", "elmish"] title: "Building mobile apps in F# using Xamarin.Forms and Elmish" images: - /blogs/building-mobile-apps-in-f-using-xamarin-forms-and-elmish/banner.png featured_image: banner.png --- # FSharp and Xamarin Xamarin is well known for allowing you to build mobile apps in C#, but you can use F# as well. F# is fully supported by the compiler and toolchains in both Visual Studio 2017 and Visual Studio for Mac. It's also pretty much supported by all the Xamarin tools including Xamarin.Forms ([except for one bug](https://github.com/xamarin/Xamarin.Forms/issues/2425) that should be fixed soon). This is great for F# fans like me, but one thing that has been missing for a while has been architecture recomendations. Mobile apps, like other UI heavy apps are very object oriented. Buttons are objects, labels are objects and they fit very nicely into the classic 'OO' space. This has meant that traditionally UI apps are also built using objects - it's objects all the way down and mutable state everywhere. This doesn't match well with functional programming paradigms which try to move away from objects and mutability. To go functional when it comes to UI, one trend is towards Elm. This is an architecture that comes from Erlang and has become popular in the F# community via [Elmish for Fable](https://github.com/elmish/elmish) for web sites. This architecture abstracts the object nature of the UI away and replaced by a purely functional model. The app runs with a function to create a view, a function to initialise an immutable model, and an update function to handle messages from either the UI or other parts of the system. The update function takes the current model and a message, and returns a new instance of the model containing a copy of the original model updated based off the message recieved. Once a new model is generated the UI is updated.
![Overview diagram of Elm](04-flow.png)
This paradigm has been ported to F# by [Don Syme](https://twitter.com/dsyme) as [Elmish.XamarinForms](https://github.com/fsprojects/Elmish.XamarinForms), an open source project also available on [NuGet](https://www.nuget.org/packages/Elmish.XamarinForms). This project provides two possible implementations of Elmish for Xamarin.Forms - known as half-elmish and full-elmish. In this post I'll look at the half-elmish implementation as this is more suited to Xamarin developers who want to move to F# (full-elmish is more suited to F# developers wanting to move to Xamarin). ### Half-Elmish Half-elmish mixes traditional Xamarin.Forms development with the elmish architecture. At it's most basic, you define your UI using XAML, setting bindings on the UI elements as if you were using MVVM. You then implement an imutable model, an update method that handles messages that you define and returns an updated model, and a view method that configures the bindings for your UI including property bindings and commands. When a message is handled the bindings are re-evaluated and the UI updates using the normal Xamarin.Forms data binding. #### Getting started Start by creating a new blank Xamarin.Forms F# app using either Visual Studio 2017 or Visual Studio for Mac called **HelloElmish**. The latest versions of both will allow you to create apps with a .NET standard core project. Add the `Elmish.XamarinForms` NuGet package to all the projects in your app. We'll create a fairly boring app, one that tracks the number of clicks of a button. Not exciting, but enough to illustrate the basics to get you started. Open the `MainPage.xaml` file, remove the contents of the `ContentPage` and add the following: ```xml