67 people like it.
Like the snippet!
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.
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
|
open System.ComponentModel
open Microsoft.FSharp.Quotations.Patterns
type ObservableObject () =
let propertyChanged =
Event<PropertyChangedEventHandler,PropertyChangedEventArgs>()
let getPropertyName = function
| PropertyGet(_,pi,_) -> pi.Name
| _ -> invalidOp "Expecting property getter expression"
interface INotifyPropertyChanged with
[<CLIEvent>]
member this.PropertyChanged = propertyChanged.Publish
member this.NotifyPropertyChanged propertyName =
propertyChanged.Trigger(this,PropertyChangedEventArgs(propertyName))
member this.NotifyPropertyChanged quotation =
quotation |> getPropertyName |> this.NotifyPropertyChanged
type MessageViewModel () =
inherit ObservableObject()
let mutable text = ""
member this.Message
with get () = text
and set value =
text <- value
this.NotifyPropertyChanged <@ this.Message @>
open System.Windows
open System.Windows.Controls
open System.Windows.Markup
let xaml =
@"<UserControl xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<TextBlock FontSize='48' Text='{Binding Message}'/>
</UserControl>"
#if INTERACTIVE
open Microsoft.TryFSharp
App.Dispatch (fun() ->
App.Console.ClearCanvas()
let view = XamlReader.Load(xaml) :?> UserControl
let viewModel = MessageViewModel(Message="Countdown")
view.DataContext <- viewModel
view |> App.Console.Canvas.Children.Add
async {
do! Async.Sleep(2000)
for i = 10 downto 1 do
viewModel.Message <- i.ToString()
do! Async.Sleep(1000)
viewModel.Message <- "Blast off!"
} |> Async.StartImmediate
App.Console.CanvasPosition <- CanvasPosition.Right
)
#endif
|
namespace System
namespace System.ComponentModel
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
module Patterns
from Microsoft.FSharp.Quotations
Multiple items
type ObservableObject =
interface INotifyPropertyChanged
new : unit -> ObservableObject
member NotifyPropertyChanged : propertyName:string -> unit
member NotifyPropertyChanged : quotation:Expr -> unit
Full name: Script.ObservableObject
--------------------
new : unit -> ObservableObject
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>
type PropertyChangedEventHandler =
delegate of obj * PropertyChangedEventArgs -> unit
Full name: System.ComponentModel.PropertyChangedEventHandler
Multiple items
type PropertyChangedEventArgs =
inherit EventArgs
new : propertyName:string -> PropertyChangedEventArgs
member PropertyName : string
Full name: System.ComponentModel.PropertyChangedEventArgs
--------------------
PropertyChangedEventArgs(propertyName: string) : unit
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
property System.Reflection.MemberInfo.Name: string
val invalidOp : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.invalidOp
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 this : ObservableObject
override ObservableObject.PropertyChanged : IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
Full name: Script.ObservableObject.PropertyChanged
property Event.Publish: IEvent<PropertyChangedEventHandler,PropertyChangedEventArgs>
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit
Full name: Script.ObservableObject.NotifyPropertyChanged
val propertyName : string
member Event.Trigger : sender:obj * args:'Args -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit
Full name: Script.ObservableObject.NotifyPropertyChanged
val quotation : Quotations.Expr
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit
Multiple items
type MessageViewModel =
inherit ObservableObject
new : unit -> MessageViewModel
member Message : string
member Message : string with set
Full name: Script.MessageViewModel
--------------------
new : unit -> MessageViewModel
val mutable text : string
val this : MessageViewModel
member MessageViewModel.Message : string with set
Full name: Script.MessageViewModel.Message
val set : elements:seq<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val value : string
property MessageViewModel.Message: string
namespace System.Windows
val xaml : string
Full name: Script.xaml
More information