51 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: 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
from Microsoft.FSharp.Quotations
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
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<_>
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)
class
inherit System.EventArgs
new : string -> System.ComponentModel.PropertyChangedEventArgs
member PropertyName : string
end
Full name: System.ComponentModel.PropertyChangedEventArgs
type: PropertyChangedEventArgs
inherits: System.EventArgs
Full name: Microsoft.FSharp.Quotations.Patterns.( |PropertyGet|_| )
type: System.Reflection.PropertyInfo
implements: System.Reflection.ICustomAttributeProvider
implements: System.Runtime.InteropServices._MemberInfo
implements: System.Runtime.InteropServices._PropertyInfo
inherits: System.Reflection.MemberInfo
Full name: Microsoft.FSharp.Core.Operators.invalidOp
type INotifyPropertyChanged =
Full name: System.ComponentModel.INotifyPropertyChanged
--------------------
INotifyPropertyChanged
class
inherit System.Attribute
new : unit -> CLIEventAttribute
end
Full name: Microsoft.FSharp.Core.CLIEventAttribute
type: CLIEventAttribute
implements: System.Runtime.InteropServices._Attribute
inherits: System.Attribute
type: ObservableObject
implements: INotifyPropertyChanged
Full name: Snippet.ObservableObject.NotifyPropertyChanged
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>
Full name: Snippet.ObservableObject.NotifyPropertyChanged
member ObservableObject.NotifyPropertyChanged : propertyName:string -> unit
member ObservableObject.NotifyPropertyChanged : quotation:Quotations.Expr -> unit
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
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>
type: MessageViewModel
implements: INotifyPropertyChanged
inherits: ObservableObject
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
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>
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: | 2 years ago |
| Author: | Phillip Trelford (website) |
| Tags: | WPF, Silverlight, MVVM |