It should be possible, I expect only syntax differences such as:

1
EventHandler handler = (EventHandler) base.Events[xxEventKey];

becoming

1
let handler = base.Events.[xxEventKey] :?> EventHandler
By on 2/26/2015 2:38 AM ()

Finally I could do it. I defined a custom event type that simulates F# Event types:

1
2
3
4
5
6
7
8
9
10
11
12
13
type ControlEvent<'a when 'a :> EventArgs>(ehl : EventHandlerList, key)  =
    member this.Publish = 
        { new IDelegateEvent<EventHandler<'a>> with
              member x.AddHandler(handler) = 
                  ehl.AddHandler (key, handler)
              
              member x.RemoveHandler(handler) = 
                  ehl.RemoveHandler (key, handler) }

    member this.Trigger (sender, args) =
         match ehl.[key] with
            | :? EventHandler<'a> as hdl -> hdl.Invoke(this, args)
            | _ -> ignore()

And it's usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type TextChangedEventArgs(oldValue, newValue) =
    inherit EventArgs()
    member this.OldValue = oldValue
    member this.NewValue = newValue

type TextBox() =
    inherit Control()
    static let textChangedKey = new obj();
    member private this.TextChangedEvent = new ControlEvent<TextChangedEventArgs>(base.Events, textChangedKey) 

    [<CLIEvent>]
    member this.TextChanged = this.TextChangedEvent.Publish

    member this.Text 
        with get () = this.ViewState.["Text"] :?> string
        and set (value : string) = 
            let oldValue = this.Text
            this.ViewState.["Text"] <- value
            this.TextChangedEvent.Trigger (this, new TextChangedEventArgs(oldValue, value))

And it works!! :D Best practices for clean code in F#?

By on 3/10/2015 9:45 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper