Skip to content

Instantly share code, notes, and snippets.

@charlesroddie
Last active May 20, 2020 11:40
Show Gist options
  • Select an option

  • Save charlesroddie/e9a895f66e4ecfb17c62d451d3347819 to your computer and use it in GitHub Desktop.

Select an option

Save charlesroddie/e9a895f66e4ecfb17c62d451d3347819 to your computer and use it in GitHub Desktop.
namespace XamarinHelpers
open Xamarin.Forms
type Grid with
member t.RowHeights
with set l =
t.RowDefinitions.Clear()
for gl in l do t.RowDefinitions.Add(RowDefinition(Height = gl))
member t.Rows with set (l: seq<seq<View>>) =
l |> Seq.iteri (fun r row ->
row |> Seq.iteri (fun i v -> if not (isNull v) then t.Children.Add(v,i,r)))
member t.ColumnWidths
with set l =
t.ColumnDefinitions.Clear()
for gl in l do t.ColumnDefinitions.Add(ColumnDefinition(Width = gl))
member t.Columns with set (l: seq<seq<View>>) =
l |> Seq.iteri (fun c col ->
col |> Seq.iteri (fun i v -> if not (isNull v) then t.Children.Add(v,c,i)))
type StackLayout with
member t.Views
with set (l: seq<View>) =
t.Children.Clear()
for c in l do
match c with | null -> () | _ -> t.Children.Add c
and get() = t.Children |> seq
type View with
member t.Tapped
with set f =
let tgr = TapGestureRecognizer()
tgr.Tapped.Add (fun _ -> 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))
type Span with
static member Link(text:string, tapped:unit->unit, ?color:Color) =
let s =
Span(Text = text,
TextDecorations = TextDecorations.Underline,
TextColor = defaultArg color Color.Blue)
let tgr = TapGestureRecognizer()
do tgr.Tapped.Add(fun _ -> tapped())
s.GestureRecognizers.Add(tgr)
s
type FormattedString with
static member OfSpans(spans:seq<Span>) =
let fs = FormattedString()
spans |> Seq.iter fs.Spans.Add
fs
module Example =
let doNothing() = ()
let grid =
let label =
let fs = FormattedString.OfSpans [Span.Link("Do nothing via a link", doNothing)]
Label(FormattedText = fs)
let stack =
StackLayout(
Views = [
Label(Text = "The second column")
Button(Text = "Do Nothing", Click = (fun () -> ()))
])
Grid(ColumnWidths = [GridLength 200.; GridLength.Star],
Rows = [[label; stack]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment