ObservableObject

The ObservableObject type implements the INotifyPropertyChanged interface used in WPF and Silverlight to notify on changes to properties that are bound to a control. Specify property names type safely using F# Quotations, i.e. <@ this.PropertyName @> when invoking the NotifyPropertyChanged method. If you are following the MVVM pattern then your View Model class can inherit from the ObservableObject type.

Copy Source
Copy Link
Tools:
 1: open System.ComponentModel
 2: open Microsoft.FSharp.Quotations.Patterns
 3: 
 4: type ObservableObject () =
 5:     let propertyChanged = 
 6:         Event<PropertyChangedEventHandler,PropertyChangedEventArgs>()
 7:     let getPropertyName = function 
 8:         | PropertyGet(_,pi,_) -> pi.Name
 9:         | _ -> invalidOp "Expecting property getter expression"
10:     interface INotifyPropertyChanged with
11:         [<CLIEvent>]
12:         member this.PropertyChanged = propertyChanged.Publish
13:     member this.NotifyPropertyChanged propertyName = 
14:         propertyChanged.Trigger(this,PropertyChangedEventArgs(propertyName))
15:     member this.NotifyPropertyChanged quotation = 
16:         quotation |> getPropertyName |> this.NotifyPropertyChanged
17: 
18: type MessageViewModel () =
19:     inherit ObservableObject()
20:     let mutable text = ""
21:     member this.Message
22:         with get () = text
23:         and set value = 
24:             text <- value
25:             this.NotifyPropertyChanged <@ this.Message @>
26: 
27: open System.Windows
28: open System.Windows.Controls
29: open System.Windows.Markup
30: 
31: let xaml = 
32:     @"<UserControl xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
33:               xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
34:        <TextBlock FontSize='48' Text='{Binding Message}'/>
35:       </UserControl>"
36: 
37: #if INTERACTIVE
38: open Microsoft.TryFSharp
39: App.Dispatch (fun() -> 
40:   App.Console.ClearCanvas()
41:   let view = XamlReader.Load(xaml) :?> UserControl
42:   let viewModel = MessageViewModel(Message="Countdown")
43:   view.DataContext <- viewModel
44:   view |> App.Console.Canvas.Children.Add
45:   async {
46:     do! Async.Sleep(2000)
47:     for i = 10 downto 1 do
48:        viewModel.Message <- i.ToString()
49:        do! Async.Sleep(1000)    
50:     viewModel.Message <- "Blast off!"
51:   } |> Async.StartImmediate
52:   App.Console.CanvasPosition <- CanvasPosition.Right
53: )
54: #endif
namespace System
namespace System.ComponentModel
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns

from Microsoft.FSharp.Quotations
type ObservableObject =
  class
    interface INotifyPropertyChanged
    new : unit -> ObservableObject
    member NotifyPropertyChanged : propertyName:string -> unit
    member NotifyPropertyChanged : quotation:Quotations.Expr -> unit
  end

Full name: Snippet.ObservableObject

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

from Microsoft.FSharp.Control

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

type Event<'Delegate,'Args (requires delegate and 'Delegate :> System.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<_>
Multiple items
type PropertyChangedEventHandler =
  delegate of obj * System.ComponentModel.PropertyChangedEventArgs -> unit

Full name: System.ComponentModel.PropertyChangedEventHandler

  type: PropertyChangedEventHandler
  implements: System.ICloneable
  implements: System.Runtime.Serialization.ISerializable
  inherits: System.MulticastDelegate
  inherits: System.Delegate


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

PropertyChangedEventHandler(obj -> PropertyChangedEventArgs -> unit)
type PropertyChangedEventArgs =
  class
    inherit System.EventArgs
    new : string -> System.ComponentModel.PropertyChangedEventArgs
    member PropertyName : string
  end

Full name: System.ComponentModel.PropertyChangedEventArgs

  type: PropertyChangedEventArgs
  inherits: System.EventArgs
val getPropertyName : (Quotations.Expr -> string)
active recognizer PropertyGet: Quotations.Expr -> (Quotations.Expr option * System.Reflection.PropertyInfo * Quotations.Expr list) option

Full name: Microsoft.FSharp.Quotations.Patterns.( |PropertyGet|_| )
val pi : System.Reflection.PropertyInfo

  type: System.Reflection.PropertyInfo
  implements: System.Reflection.ICustomAttributeProvider
  implements: System.Runtime.InteropServices._MemberInfo
  implements: System.Runtime.InteropServices._PropertyInfo
  inherits: System.Reflection.MemberInfo
property System.Reflection.MemberInfo.Name: string
val invalidOp : string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidOp
Multiple items
type INotifyPropertyChanged =

Full name: System.ComponentModel.INotifyPropertyChanged

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

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

Full name: Microsoft.FSharp.Core.CLIEventAttribute

  type: CLIEventAttribute
  implements: System.Runtime.InteropServices._Attribute
  inherits: System.Attribute
val this : ObservableObject

  type: ObservableObject
  implements: INotifyPropertyChanged
property Event.Publish: IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit

Full name: Snippet.ObservableObject.NotifyPropertyChanged
val propertyName : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
member Event.Trigger : sender:obj * args:'Args -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit

Full name: Snippet.ObservableObject.NotifyPropertyChanged
val quotation : Quotations.Expr
Multiple overloads
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit
type MessageViewModel =
  class
    inherit ObservableObject
    new : unit -> MessageViewModel
    member Message : string
    member Message : string with set
  end

Full name: Snippet.MessageViewModel

  type: MessageViewModel
  implements: INotifyPropertyChanged
  inherits: ObservableObject
val mutable text : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
val this : MessageViewModel

  type: MessageViewModel
  implements: INotifyPropertyChanged
  inherits: ObservableObject
property MessageViewModel.Message: string
val set : seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val value : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
namespace System.Windows
namespace System.Windows.Controls
namespace System.Windows.Markup
val xaml : string

Full name: Snippet.xaml

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>

More information

Link: http://fssnip.net/2x
Posted: 3 years ago
Author: Phillip Trelford (website)
Tags: WPF, Silverlight, MVVM