Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

💅 Extensions to make MvvmCross bindings more F#-ish

Notifications You must be signed in to change notification settings

heytherewill/MvvmCross.FSharp

Repository files navigation

MvvmCross.FSharp

💅 Extensions to make MvvmCross bindings more F#-ish

Installation

Install via NuGet using:

PM> Install-Package MvvmCross.FSharp

wat?

This is what bindings in F# look like:

let set = this.CreateBindingSet<MyView, MyViewModel>()

set
  .Bind(source)
  .To("MyList") 
  |> ignore

set
  .Bind(source)
  .For("SelectionChangedCommand")
  .To("MyCommand") 
  |> ignore
  
set.Apply()

MvvmCross.FSharp® allows you to use quotations instead of strings for additional type safety:

let set = this.CreateBindingSet<MyView, MyViewModel>()

set
|> bind source
|> toExpression <@ this.ViewModel.MyList @>
|> ignore

set
|> bind source
|> forExpression <@ source.SelectionChangedCommand @>
|> toExpression <@ this.ViewModel.MyCommand @>
|> ignore

apply set

<| ALSO PIPES |>

DSL

You can also use a DSL based on the operators:

  • |>>: The bind operator
  • >>: The to operator
  • >>>>: The for operator

Note that >> = to = TWO characters. The same applies to the for operator. Mnemonics FTW.

To use plain text instead of quotations when using the custom operators, replace the last > with a .. The number of characters in the operator remains the same, so it's still easy to remember.

Here's what the sample bindings would look like using the operator DSL:

let set = this.CreateBindingSet<MyView, MyViewModel>()
set |>> source >> <@ this.ViewModel.MyList @> |> ignore
set |>> source >>>> <@ source.SelectionChangedCommand @> >> <@ this.ViewModel.MyCommand @> |> ignore
apply set

Framework provided bindings

A script scrapes the MvvmCross source code and generates a source file that provides type safe for methods for each of the default-framework-provided bindings. This allows you to use bindings that are provided by the framework in a type safe way, like this:

open MvvmCross.FSharp.iOS.PropertyBinding

set
|> bind myButton
|> forTouchDown
|> toExpression <@ this.ViewModel.MyCommand @> 
|> ignore

What's next?

  • Make the DSL even better®
  • Turn the scrape script into a Type Provider

About

💅 Extensions to make MvvmCross bindings more F#-ish

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published