-
Notifications
You must be signed in to change notification settings - Fork 0
/
IosPlatformView.fs
48 lines (42 loc) · 2.28 KB
/
IosPlatformView.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
namespace Fabulous.XamarinNative
open Fabulous.XamarinNative
open System
open System.Reflection
open UIKit
type IosPlatformView<'model, 'msg>(m: 'model, dispatch: 'msg -> unit, propMap: ViewBindings<'model, 'msg>, viewController: IProgramHost, debug: bool) =
inherit PlatformView<'model, 'msg>(m, dispatch, propMap, viewController, debug)
let getUiElement (viewController: UIViewController) (elementName: string) =
let propInfo = viewController.GetType().GetProperty(elementName, BindingFlags.NonPublic ||| BindingFlags.Instance)
if propInfo <> null then
propInfo.GetValue(viewController)
else
null
override this.bind (host: IProgramHost) (elementName: string) (value: obj) =
let element = getUiElement (host :?> UIViewController) elementName
match element with
| :? UILabel as label -> label.Text <- value.ToString()
| :? UITextField as textField -> textField.Text <- value.ToString()
| :? UISlider as slider -> slider.SetValue(float32 (value :?> int), false)
| null ->
let propInfo = host.GetType().GetProperty(elementName, BindingFlags.Public ||| BindingFlags.Instance)
propInfo.SetValue(host, value)
| _ -> ()
override this.bindCmd (host: IProgramHost) (elementName: string) (dispatch: 'msg -> unit) (msg: 'msg) =
let element = getUiElement (host :?> UIViewController) elementName
match element with
| :? UIControl as uiControl -> uiControl.TouchDown.Add(fun _ -> dispatch msg)
| _ -> ()
()
override this.bindValueChanged (host: IProgramHost) (model: 'model) (elementName: string) (dispatch: 'msg -> unit) (setter: Setter<'model,'msg>) =
let element = getUiElement (host :?> UIViewController) elementName
match element with
| :? UITextField as textField ->
textField.AddTarget(EventHandler (fun sender event ->
dispatch <| setter textField.Text model)
, UIControlEvent.EditingChanged)
| :? UISlider as slider ->
slider.AddTarget(EventHandler (fun sender event ->
let value = int(slider.Value + 0.5f)
dispatch <| setter value model)
, UIControlEvent.ValueChanged)
| _ -> ()