You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -57,18 +56,91 @@ Steps to create a new project:
57
56
5) Replace the [`ViewLocator.fs`](https://github.com/JordanMarr/Elmish.Avalonia/blob/main/src/Samples/AvaloniaExample/ViewLocator.fs) with the one from from the [AvaloniaExample project](https://github.com/JordanMarr/Elmish.Avalonia/tree/main/src/Samples/AvaloniaExample). This makes it easier to bind the view/viewmodel and start the Elmish loop using convention.
58
57
Looking at the [AvaloniaExample project](https://github.com/JordanMarr/Elmish.Avalonia/tree/main/src/Samples/AvaloniaExample), this allows us to bind the `MainView.axaml``Content` via the `ViewLocator` to locate the appropriate view and start the Elmish loop.
59
58
60
-
# AvaloniaProgram
61
-
The `AvaloniaProgram` contains functions that configure an Elmish program.
59
+
# Sample Project
60
+
Please view the [AvaloniaExample project](https://github.com/JordanMarr/Elmish.Avalonia/tree/main/src/Samples/AvaloniaExample).
62
61
63
-
*`AvaloniaProgram.startElmishLoop` - Starts the Elmish loop and binds the given view to the bindings.
62
+
# AvaloniaProgram
63
+
The `AvaloniaProgram` module contains functions that configure an Elmish program.
64
+
65
+
### AvaloniaProgram.startElmishLoop
66
+
Starts the Elmish loop and binds the given view to the bindings.
67
+
68
+
```F#
69
+
let start view =
70
+
AvaloniaProgram.mkProgram init update bindings
71
+
|> AvaloniaProgram.startElmishLoop view
72
+
```
73
+
74
+
### AvaloniaProgram.withSubscription
75
+
Creates one or more Elmish subscriptions that can dispatch messages and be enabled/disabled based on the model.
76
+
77
+
```F#
78
+
let subscriptions (model: Model) : Sub<Msg> =
79
+
let autoUpdateSub (dispatch: Msg -> unit) =
80
+
let timer = new System.Timers.Timer(1000)
81
+
let disposable =
82
+
timer.Elapsed.Subscribe(fun _ ->
83
+
let randomNull = rnd.Next(0, 99)
84
+
match randomNull with
85
+
| i when i = 0 ->
86
+
dispatch AddNull
87
+
| _ ->
88
+
dispatch AddItem
89
+
dispatch RemoveItem
90
+
)
91
+
timer.Start()
92
+
disposable
93
+
94
+
[
95
+
if model.IsAutoUpdateChecked then
96
+
[ nameof autoUpdateSub ], autoUpdateSub
97
+
]
98
+
99
+
100
+
let vm =
101
+
AvaloniaProgram.mkSimple init update bindings
102
+
|> AvaloniaProgram.withSubscription subscriptions
103
+
|> ElmishViewModel.create
104
+
```
64
105
65
106
# ElmishViewModel
66
-
The `ElmishViewModel` contains functions that configure an `IElmishViewModel`.
107
+
The `ElmishViewModel`module contains functions that configure an `IElmishViewModel`.
67
108
The `IElmishViewModel` has a single method, `StartElmishLoop`, which takes an Avalonia view, binds it with the bindings and starts the Elmish loop.
68
109
Use of the `ElmishViewModel` is optional and exists primarily to facilitate the `ViewLocator` pattern.
69
110
70
-
*`ElmishViewModel.create` - Creates an `ElmishViewModel<'model, 'msg>`
71
-
72
-
*`ElmishViewModel.terminateOnViewUnloaded` - Creates an Elmish subscription when the view `Unloaded` event fires that dispatches the passed-in termination `'msg` to terminate the Elmish loop.
73
-
74
-
*`ElmishViewModel.subscribe` - Adds an Elmish subscription.
111
+
### ElmishViewModel.create
112
+
Creates an `ElmishViewModel<'model, 'msg>`
113
+
114
+
```F#
115
+
let vm =
116
+
AvaloniaProgram.mkProgram init update bindings
117
+
|> ElmishViewModel.create
118
+
```
119
+
120
+
121
+
### ElmishViewModel.terminateOnViewUnloaded
122
+
Creates an Elmish subscription when the view `Unloaded` event fires that dispatches the passed-in termination `'msg` to terminate the Elmish loop.
123
+
NOTE: You must create a `'Msg` that will be registered to trigger loop termination.
0 commit comments