F# Quotations with INotifyPropertyChanged

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

Copy Source
Copy Link
Tools:
 1: open System
 2: open System.Collections.ObjectModel
 3: open System.ComponentModel
 4: open Microsoft.FSharp.Quotations
 5: open Microsoft.FSharp.Quotations.Patterns
 6: 
 7: type ViewModelBase() =
 8:     let propertyChanged = new Event<_, _>()
 9:     let toPropName(query : Expr) = 
10:         match query with
11:         | PropertyGet(a, b, list) ->
12:             b.Name
13:         | _ -> ""
14: 
15:     interface INotifyPropertyChanged with
16:         [<CLIEvent>]
17:         member x.PropertyChanged = propertyChanged.Publish
18: 
19:     abstract member OnPropertyChanged: string -> unit
20:     default x.OnPropertyChanged(propertyName : string) =
21:         propertyChanged.Trigger(x, new PropertyChangedEventArgs(propertyName))
22: 
23:     member x.OnPropertyChanged(expr : Expr) =
24:         let propName = toPropName(expr)
25:         x.OnPropertyChanged(propName)
26: 
27: type TestModel() =
28:     inherit ViewModelBase()
29: 
30:     let mutable selectedItem : obj = null
31: 
32:     member x.SelectedItem
33:         with get() = selectedItem
34:         and set(v : obj) = 
35:             selectedItem <- v
36:             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
type ViewModelBase =
  class
    interface INotifyPropertyChanged
    new : unit -> ViewModelBase
    abstract member OnPropertyChanged : string -> unit
    override OnPropertyChanged : propertyName:string -> unit
    member OnPropertyChanged : expr:Expr -> unit
  end

Full name: Snippet.ViewModelBase

  type: ViewModelBase
  implements: INotifyPropertyChanged
val propertyChanged : Event<PropertyChangedEventHandler,PropertyChangedEventArgs>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------

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

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

--------------------

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

Full name: Microsoft.FSharp.Control.Event<_>
val toPropName : (Expr -> string)
val query : Expr
Multiple items
type Expr<'T> =
  class
    inherit Expr
    member Raw : Expr
  end

Full name: Microsoft.FSharp.Quotations.Expr<_>

  type: Expr<'T>
  inherits: Expr


--------------------

type Expr =
  class
    override Equals : obj:obj -> bool
    member GetFreeVars : unit -> seq<Var>
    member Substitute : substitution:(Var -> Expr option) -> Expr
    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
    static member Call : methodInfo:Reflection.MethodInfo * arguments:Expr list -> Expr
    static member Call : obj:Expr * methodInfo:Reflection.MethodInfo * arguments:Expr list -> Expr
    static member Cast : source:Expr -> Expr<'T>
    static member Coerce : source:Expr * target:Type -> Expr
    static member DefaultValue : expressionType:Type -> Expr
    static member Deserialize : qualifyingType:Type * spliceTypes:Type list * spliceExprs:Expr list * bytes:byte [] -> Expr
    static member FieldGet : fieldInfo:Reflection.FieldInfo -> Expr
    static member FieldGet : obj:Expr * fieldInfo:Reflection.FieldInfo -> Expr
    static member FieldSet : fieldInfo:Reflection.FieldInfo * value:Expr -> Expr
    static member FieldSet : obj:Expr * fieldInfo:Reflection.FieldInfo * value:Expr -> Expr
    static member ForIntegerRangeLoop : loopVariable:Var * start:Expr * endExpr:Expr * body:Expr -> Expr
    static member GlobalVar : name:string -> Expr<'T>
    static member IfThenElse : guard:Expr * thenExpr:Expr * elseExpr:Expr -> Expr
    static member Lambda : parameter:Var * body:Expr -> Expr
    static member Let : letVariable:Var * letExpr:Expr * body:Expr -> Expr
    static member LetRecursive : bindings:(Var * Expr) list * body:Expr -> Expr
    static member NewArray : elementType:Type * elements:Expr list -> Expr
    static member NewDelegate : delegateType:Type * parameters:Var list * body:Expr -> Expr
    static member NewObject : constructorInfo:Reflection.ConstructorInfo * arguments:Expr list -> Expr
    static member NewRecord : recordType:Type * elements:Expr list -> Expr
    static member NewTuple : elements:Expr list -> Expr
    static member NewUnionCase : unionCase:Reflection.UnionCaseInfo * arguments:Expr list -> Expr
    static member PropertyGet : property:Reflection.PropertyInfo * ?indexerArgs:Expr list -> Expr
    static member PropertyGet : obj:Expr * property:Reflection.PropertyInfo * ?indexerArgs:Expr list -> Expr
    static member PropertySet : property:Reflection.PropertyInfo * value:Expr * ?indexerArgs:Expr list -> Expr
    static member PropertySet : obj:Expr * property:Reflection.PropertyInfo * value:Expr * ?indexerArgs:Expr list -> Expr
    static member Quote : inner:Expr -> Expr
    static member RegisterReflectedDefinitions : assembly:Reflection.Assembly * resource:string * serializedValue:byte [] -> unit
    static member Sequential : first:Expr * second:Expr -> Expr
    static member TryFinally : body:Expr * compensation:Expr -> Expr
    static member TryGetReflectedDefinition : methodBase:Reflection.MethodBase -> Expr option
    static member TryWith : body:Expr * filterVar:Var * filterBody:Expr * catchVar:Var * catchBody:Expr -> Expr
    static member TupleGet : tuple:Expr * index:int -> Expr
    static member TypeTest : source:Expr * target:Type -> Expr
    static member UnionCaseTest : source:Expr * unionCase:Reflection.UnionCaseInfo -> Expr
    static member Value : value:'T -> Expr
    static member Value : value:obj * expressionType:Type -> Expr
    static member Var : variable:Var -> Expr
    static member VarSet : variable:Var * value:Expr -> Expr
    static member WhileLoop : guard:Expr * body:Expr -> Expr
  end

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

  type: Expr option
  implements: Collections.IStructuralEquatable
  implements: IComparable<Option<Expr>>
  implements: IComparable
  implements: Collections.IStructuralComparable
val b : Reflection.PropertyInfo

  type: Reflection.PropertyInfo
  implements: Reflection.ICustomAttributeProvider
  implements: Runtime.InteropServices._MemberInfo
  implements: Runtime.InteropServices._PropertyInfo
  inherits: Reflection.MemberInfo
Multiple items
val list : Expr list

  type: Expr list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<Expr>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<Expr>
  implements: Collections.IEnumerable


--------------------

type 'T list = List<'T>

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

  type: 'T list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<'T>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<'T>
  implements: Collections.IEnumerable
property Reflection.MemberInfo.Name: string
Multiple items
type INotifyPropertyChanged =

Full name: System.ComponentModel.INotifyPropertyChanged

--------------------

INotifyPropertyChanged
type CLIEventAttribute =
  class
    inherit Attribute
    new : unit -> CLIEventAttribute
  end

Full name: Microsoft.FSharp.Core.CLIEventAttribute

  type: CLIEventAttribute
  implements: Runtime.InteropServices._Attribute
  inherits: Attribute
Multiple items
val x : ViewModelBase

  type: ViewModelBase
  implements: INotifyPropertyChanged


--------------------

val x : ViewModelBase

  type: ViewModelBase
  implements: INotifyPropertyChanged


--------------------

val x : ViewModelBase

  type: ViewModelBase
  implements: INotifyPropertyChanged
property Event.Publish: IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
abstract member ViewModelBase.OnPropertyChanged : string -> unit

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

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

--------------------

type string = String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
type unit = Unit

Full name: Microsoft.FSharp.Core.unit

  type: unit
  implements: IComparable
val x : ViewModelBase

  type: ViewModelBase
  implements: INotifyPropertyChanged
override ViewModelBase.OnPropertyChanged : propertyName:string -> unit

Full name: Snippet.ViewModelBase.OnPropertyChanged
val propertyName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
member Event.Trigger : sender:obj * args:'Args -> unit
type PropertyChangedEventArgs =
  class
    inherit System.EventArgs
    new : string -> System.ComponentModel.PropertyChangedEventArgs
    member PropertyName : string
  end

Full name: System.ComponentModel.PropertyChangedEventArgs

  type: PropertyChangedEventArgs
  inherits: EventArgs
member ViewModelBase.OnPropertyChanged : expr:Expr -> unit

Full name: Snippet.ViewModelBase.OnPropertyChanged
val expr : Expr
val propName : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
Multiple overloads
abstract member ViewModelBase.OnPropertyChanged : string -> unit
member ViewModelBase.OnPropertyChanged : expr:Expr -> unit
type TestModel =
  class
    inherit ViewModelBase
    new : unit -> TestModel
    member SelectedItem : obj
    member SelectedItem : obj with set
  end

Full name: Snippet.TestModel

  type: TestModel
  implements: INotifyPropertyChanged
  inherits: ViewModelBase
val mutable selectedItem : obj
type obj = Object

Full name: Microsoft.FSharp.Core.obj
Multiple items
val x : TestModel

  type: TestModel
  implements: INotifyPropertyChanged
  inherits: ViewModelBase


--------------------

val x : TestModel

  type: TestModel
  implements: INotifyPropertyChanged
  inherits: ViewModelBase
property TestModel.SelectedItem: obj
val set : seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : obj
val x : TestModel

  type: TestModel
  implements: INotifyPropertyChanged
  inherits: ViewModelBase

More information

Link: http://fssnip.net/4Q
Posted: 9 months ago
Author: Fahad
Tags: F#, WPF, Silverlight