views:

344

answers:

2

I'm trying to add & remove an event handler from a Silverlight FrameworkElement, but I just can't get the syntax correct.

I want to do something like this:

let clickedEventHandler = fun args -> printfn "Click!"
fe.MouseLeftButtonDown.AddHandler( clickedEventHandler)
...
fe.MouseLeftButtonDown.RemoveHandler(clickedEventHandler)

What should be the correct way to do this?

+1  A: 

In this case, it turns out you can do the following:

let clickedEventHandler = new MouseButtonEventHandler(fun obj args -> printfn "Click!")
fe.MouseLeftButtonDown.AddHandler( clickedEventHandler)
...
fe.MouseLeftButtonDown.RemoveHandler(clickedEventHandler)
RodYan
+3  A: 

As you already mentioned, you need to create an instance of delegate:

let ch = new MouseButtonEventHandler(fun obj args ->
  printfn "Click!") 

Then you can use ch as an argument to AddHandler or RemoveHanlder. The reason is, that F# function value isn't actually represented as any delegate type. It has it's own type, which isn't a delegate (see also another SO discussion). You can see the difference if you look at the types in F# interactive (or VS IntelliSense):

> (fun (o:obj) (a:MouseButtonEventArgs) -> printfn "Click!") 
val it : obj -> MouseButtonEventArgs -> unit = (...)

> new MouseButtonEventHandler(fun o a -> printfn "Click!") 
val it : MouseButtonEventHandler = (...)

What may be confusing is that F# also allows implicit conversion from lambda function value to a compatible delegate type when calling a method, however this works only when you give a lambda function directly as the argument (I think):

fe.MouseLeftButtonDown.AddHandler(fun obj args -> 
    printfn "Click!")        

Another reason why you still need delegates in F# is that delegates have a well-defined comparison semantics (they are compared as any other reference types). For functions, the F# specification doesn't say when they are equal.

Tomas Petricek