views:

142

answers:

1

How would one implement INotifyPropertyChanged for use in an F# type? Thanks!

+1  A: 

It's basically the same as in any other language:

open System.ComponentModel
type MyType() =
  let ev = new Event<_,_>()
  let mutable str = ""
  member x.StringProp
    with get() = str
     and set(str') =
       str <- str'
       ev.Trigger(x, PropertyChangedEventArgs("StringProp"))
  interface INotifyPropertyChanged with
    [<CLIEvent>]
    member x.PropertyChanged = ev.Publish
kvb
That is not "basically the same" as other languages :P
Cameron MacFarland
Thanks,What exactly is the [<CLIEvent>] attribute do? I can't seem to find any the documentation on it.
RodYan
@RodYan - it affects the compiled form that the event takes; for interop with other .NET languages (and to implement interfaces exposing .NET events), you'll need to apply it to an IEvent value. This causes `add_` and `remove_` methods to be generated, as opposed to actually exposing a property of type `IEvent<_,_>`, as described at http://msdn.microsoft.com/en-us/library/ee370437(VS.100).aspx.
kvb