5.8 KiB
F# Functional App Development, using Xamarin.Forms
Never write a ViewModel class again! Conquer the world with clean dynamic UIs!
This library allows you to use a variation of elmish, an Elm architecture implemented in F#, to build Xamarin.Forms applications for iOS, Android, Mac and more.
The amount of code I'm not writing is great! @jimbobbennett
This project is a sample and may change.
{% include_relative contents.md %}
Getting started
-
Enable Xamarin support in Visual Studio or Visual Studio for Mac.
-
Install the template pack:
dotnet new -i Elmish.XamarinForms.Templates
-
Create a blank F# Functional Xamarin Forms app:
dotnet new elmish-forms-app -lang F# -n SqueakyApp
-
Open, edit, build and deploy in Visual Studio, Visual Studio for Mac and/or "msbuild" command line
SqueakyApp/SqueakyApp.sln
A Basic Example
Here is a full example of an app:
/// The messages dispatched by the view
type Msg =
| Pressed
/// The model from which the view is generated
type Model =
{ Pressed: bool }
/// Returns the initial state
let init() = { Pressed=false }
/// The funtion to update the view
let update (msg:Msg) (model:Model) =
match msg with
| Pressed -> { model with Pressed = true }
/// The view function giving updated content for the page
let view (model: Model) dispatch =
if model.Pressed then
View.Label(text="I was pressed!")
else
View.Button(text="Press Me!", command=(fun () -> dispatch Pressed))
type App () =
inherit Application ()
let runner =
Program.mkSimple init update view
|> Program.withConsoleTrace
|> Program.runWithDynamicView
The init function returns your initial state, and each model gets an update function for message processing. The view
function computes an immutable Xaml-like description. In the above example, the choice between a label and button depends on the model.Pressed
value.
Some advantages of using an immutable model are:
- It is easy to unit test your
init
,update
andview
functions - You can save/restore your model relatively easily
- It makes tracing causality usually very simple
Samples
The sample CounterApp contains a slightly larger example of Button/Label/Slider elements.
The sample TicTacToe contains examples of the Grid and Image elements.
The sample AllControls contains examples of instantiating most elements in Xamarin.Forms.Core
.
The external sample Calculator is a small calculator app. (Note: because this is an external sample it may not be up-to-date with the latest version of his library.)
The external sample PocketPiggyBank is a small client-server app with login authentication. (Note: because this is an external sample it may not be up-to-date with the latest version of this library.)
Further Resources
General Docs
Android Setup
iOS Setup
Contributing
Please contribute to this library through issue reports, pull requests, code reviews and discussion.