Skip to content

Instantly share code, notes, and snippets.

@GuyCarver
Last active July 4, 2018 09:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuyCarver/76c3af18a4761d022928b44a6ab94aa0 to your computer and use it in GitHub Desktop.
Save GuyCarver/76c3af18a4761d022928b44a6ab94aa0 to your computer and use it in GitHub Desktop.
F# version of tipcalculator.cs example for Continuous app
//
// Tip Calculator Example
//
// Calculates a tip and total given a bill amount
//
open System;
open Xamarin.Forms;
let clr1 = Color.FromRgb (150, 200, 190)
let mutable tipperc = 20.0
let title = Label (
Text = "Tip Calculator",
FontSize = 16.0,
FontFamily = "Zapfino",
HorizontalOptions = LayoutOptions.Center,
Margin = Thickness(0.0, 64.0, 0.0, 0.0)
)
let billEntry = Entry (
Text = "12.43",
FontSize = 32.0,
Placeholder = (0.0).ToString("C"),
HeightRequest = 44.0,
HorizontalTextAlignment = TextAlignment.Center,
Margin = Thickness(50.0, 0.0, 50.0, 0.0)
)
let percent = Picker (
Title = "%",
HorizontalOptions = LayoutOptions.Center
//VerticalOptions = LayoutOptions.CenterAndExpand
)
let perc = [10.0; 15.0; 18.0; 20.0; 25.0; 30.0]
let addit v =
match v with
| [] -> ()
| x::xs -> percent.Items.Add x.ToString()
addit xs
addit perc
let tip = Label (
Text = "Tip",
FontSize = 28.0,
HorizontalOptions = LayoutOptions.Center
)
let total = Label (
Text = "Total",
FontSize = 28.0,
HorizontalOptions = LayoutOptions.Center
)
let UpdateOutput () =
let valtext = billEntry.Text
//printfn valtext
let mutable value = Double.Parse (billEntry.Text)
let tipval = Math.Round (value * tipperc) / 100.0
tip.Text <- "Tip " + tipval.ToString "C"
let tt = value + tipval
total.Text <- "Total " + tt.ToString "C"
billEntry.TextChanged.Add ( fun (a : TextChangedEventArgs ) ->
UpdateOutput ()
)
UpdateOutput ()
percent.SelectedIndexChanged.Add (fun (a : EventArgs) ->
tipperc <- match percent.SelectedIndex with
| -1 -> 20.0
| _ -> perc.Item percent.SelectedIndex
UpdateOutput ()
)
percent.SelectedIndex <- 3
let pageStack = StackLayout()
let outs = StackLayout (
Margin = Thickness(50.0, 0.0, 50.0, 0.0)
)
outs.Children.Add tip
outs.Children.Add total
outs.BackgroundColor <- clr1.WithLuminosity 0.8
pageStack.Children.Add title
pageStack.Children.Add billEntry
pageStack.Children.Add percent
pageStack.Children.Add outs
pageStack.BackgroundColor <- clr1
let Main = ContentPage (
Content = pageStack
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment