/
Program.fs
52 lines (43 loc) · 2.27 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace ElmInspiredOne
open Gjallarhorn.Bindable
open Gjallarhorn.Bindable.Framework
// Note that this program is defined in a PCL, and is completely platform neutral.
// It will work unchanged on WPF, Xamarin Forms, etc
module Program =
// ---------------------------------- Model ----------------------------------
// Model is a simple integer for counter
type Model = { Value : int }
// Create an initialization function
let initModel i = { Value = i }
// ---------------------------------- Update ----------------------------------
// We define a union type for each possible message
type Msg =
| Increment
| Decrement
// Create a function that updates the model given a message
let update msg model =
match msg with
| Increment -> { Value = min 10 (model.Value + 1) }
| Decrement -> { Value = max 0 (model.Value - 1) }
// Our "ViewModel". This is optional, but allows you to avoid using "magic strings", as well as enables design time XAML in C# projects
type ViewModel =
{
Current : int
Increment : VmCmd<Msg>
Decrement : VmCmd<Msg>
}
// This is our design/compile time ViewModel used for XAML and binding for naming
let d = { Current = 5 ; Increment = Vm.cmd Increment; Decrement = Vm.cmd Decrement }
// ---------------------------------- Binding ----------------------------------
// Create a function that binds a model to a source, and outputs messages
// This essentially acts as our "view" in Elm terminology, though it doesn't actually display
// the view as much as map from our type to bindings
let bindToSource =
// Create our bindings - the VM type defines the name, the Bind call determines the type of data binding
Component.create [
<@ d.Current @> |> Bind.oneWay (fun m -> m.Value)
<@ d.Increment @> |> Bind.cmdIf (fun m -> m.Value < 10)
<@ d.Decrement @> |> Bind.cmdIf (fun m -> m.Value > 0)
]
// ---------------------------------- Framework -----------------------------------
let applicationCore = Framework.application (initModel 5) update bindToSource Nav.empty