2 people like it.

Some Xamarin.Forms helper extension methods

For clean initialization of Grids, StackLayouts etc., and other Views with simple click handlers

 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: 
open Xamarin.Forms

type Grid with
    member t.ColumnWidths
        with set l =
            let cd = ColumnDefinitionCollection()
            for gl in l do cd.Add(ColumnDefinition(Width = gl))
            t.ColumnDefinitions <- cd
    member t.RowHeights
        with set l =
            let rd = RowDefinitionCollection()
            for gl in l do rd.Add(RowDefinition(Height = gl))
            t.RowDefinitions <- rd
    member t.Cells
        with set l =
            t.Children.Clear()
            for (v,c,r) in l do t.Children.Add(v,c,r)
    /// NB doesn't clear previous items
    member t.FirstRow
        with set l =
            l |> Seq.iteri (fun i v -> t.Children.Add(v,i,0))
    /// NB doesn't clear previous items
    member t.FirstColumn
        with set l =
            l |> Seq.iteri (fun i v -> t.Children.Add(v,0,i))

type StackLayout with
    member t.Views
        with set (l: seq<View>) =
            t.Children.Clear()
            for c in l do t.Children.Add c
        and get() = t.Children |> seq

type View with
    /// NB does not remove any previous handlers
    member t.Tapped
        with set f =
            let tgr = TapGestureRecognizer()
            tgr.Tapped.Add(f)
            t.GestureRecognizers.Add(tgr)

type Button with
    member t.Click
        with set f =
            t.Clicked.Add (fun _ -> f())

type Switch with
    member t.Toggle
        with set f = t.Toggled.Add (fun x -> f(x.Value))
namespace Xamarin
namespace Xamarin.Forms
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
module Seq

from Microsoft.FSharp.Collections
val iteri : action:(int -> 'T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iteri
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>

More information

Link:http://fssnip.net/7SJ
Posted:6 years ago
Author:Charles Roddie
Tags: xamarin