12 people like it.

F# Quotations with INotifyPropertyChanged

ViewModelBase for F# users who want to use it in WPF / Silverlight

 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: 
open System
open System.Collections.ObjectModel
open System.ComponentModel
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

type ViewModelBase() =
    let propertyChanged = new Event<_, _>()
    let toPropName(query : Expr) = 
        match query with
        | PropertyGet(a, b, list) ->
            b.Name
        | _ -> ""

    interface INotifyPropertyChanged with
        [<CLIEvent>]
        member x.PropertyChanged = propertyChanged.Publish

    abstract member OnPropertyChanged: string -> unit
    default x.OnPropertyChanged(propertyName : string) =
        propertyChanged.Trigger(x, new PropertyChangedEventArgs(propertyName))

    member x.OnPropertyChanged(expr : Expr) =
        let propName = toPropName(expr)
        x.OnPropertyChanged(propName)

type TestModel() =
    inherit ViewModelBase()

    let mutable selectedItem : obj = null

    member x.SelectedItem
        with get() = selectedItem
        and set(v : obj) = 
            selectedItem <- v
            x.OnPropertyChanged(<@ x.SelectedItem @>)
namespace System
namespace System.Collections
namespace System.Collections.ObjectModel
namespace System.ComponentModel
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns

from Microsoft.FSharp.Quotations
Multiple items
type ViewModelBase =
  interface INotifyPropertyChanged
  new : unit -> ViewModelBase
  abstract member OnPropertyChanged : string -> unit
  override OnPropertyChanged : propertyName:string -> unit
  member OnPropertyChanged : expr:Expr -> unit

Full name: Script.ViewModelBase

--------------------
new : unit -> ViewModelBase
val propertyChanged : Event<PropertyChangedEventHandler,PropertyChangedEventArgs>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val toPropName : (Expr -> string)
val query : Expr
Multiple items
type Expr =
  override Equals : obj:obj -> bool
  member GetFreeVars : unit -> seq<Var>
  member Substitute : substitution:(Var -> Expr option) -> Expr
  member ToString : full:bool -> string
  member CustomAttributes : Expr list
  member Type : Type
  static member AddressOf : target:Expr -> Expr
  static member AddressSet : target:Expr * value:Expr -> Expr
  static member Application : functionExpr:Expr * argument:Expr -> Expr
  static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
  ...

Full name: Microsoft.FSharp.Quotations.Expr

--------------------
type Expr<'T> =
  inherit Expr
  member Raw : Expr

Full name: Microsoft.FSharp.Quotations.Expr<_>
active recognizer PropertyGet: Expr -> (Expr option * Reflection.PropertyInfo * Expr list) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |PropertyGet|_| )
val a : Expr option
val b : Reflection.PropertyInfo
Multiple items
val list : Expr list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
property Reflection.MemberInfo.Name: string
type INotifyPropertyChanged =
  event PropertyChanged : PropertyChangedEventHandler

Full name: System.ComponentModel.INotifyPropertyChanged
Multiple items
type CLIEventAttribute =
  inherit Attribute
  new : unit -> CLIEventAttribute

Full name: Microsoft.FSharp.Core.CLIEventAttribute

--------------------
new : unit -> CLIEventAttribute
val x : ViewModelBase
override ViewModelBase.PropertyChanged : IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>

Full name: Script.ViewModelBase.PropertyChanged
property Event.Publish: IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
abstract member ViewModelBase.OnPropertyChanged : string -> unit

Full name: Script.ViewModelBase.OnPropertyChanged
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
override ViewModelBase.OnPropertyChanged : propertyName:string -> unit

Full name: Script.ViewModelBase.OnPropertyChanged
val propertyName : string
member Event.Trigger : sender:obj * args:'Args -> unit
Multiple items
type PropertyChangedEventArgs =
  inherit EventArgs
  new : propertyName:string -> PropertyChangedEventArgs
  member PropertyName : string

Full name: System.ComponentModel.PropertyChangedEventArgs

--------------------
PropertyChangedEventArgs(propertyName: string) : unit
member ViewModelBase.OnPropertyChanged : expr:Expr -> unit

Full name: Script.ViewModelBase.OnPropertyChanged
val expr : Expr
val propName : string
override ViewModelBase.OnPropertyChanged : propertyName:string -> unit
member ViewModelBase.OnPropertyChanged : expr:Expr -> unit
Multiple items
type TestModel =
  inherit ViewModelBase
  new : unit -> TestModel
  member SelectedItem : obj
  member SelectedItem : obj with set

Full name: Script.TestModel

--------------------
new : unit -> TestModel
val mutable selectedItem : obj
type obj = Object

Full name: Microsoft.FSharp.Core.obj
val x : TestModel
member TestModel.SelectedItem : obj with set

Full name: Script.TestModel.SelectedItem
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : obj
property TestModel.SelectedItem: obj
Raw view New version

More information

Link:http://fssnip.net/4Q
Posted:5 years ago
Author:Fahad
Tags: f# , wpf , silverlight