F#中的事件处理确实给我带来了麻烦。我面临的错误是
This expression was expected to have type
obj -> unit
but here has type
System.EventHandler<obj>
我按照以下方式发布活动
type ...
let dataChanged = new Control.Event<obj>()
[<CLIEvent>]
member this.DataChanged = dataChanged.Publish
member private this.NotifyDataChanged(sender : obj) =
dataChanged.Trigger(sender)
我试图像这样抓住它
type ...
let dataChangedHandler = new System.EventHandler<obj>(fun sender -> this.DataItems_DataChanged(sender))
do this.DataItems.DataChanged.Add(dataChangedHandler)
member private this.DataItems_DataChanged(sender : obj) =
...
如上所述,代码没有编译。我尝试过不同的变化,但却无法让它发挥作用。任何帮助将不胜感激。
答案 0 :(得分:1)
编译器清楚地告诉您:dataChangedHandler
应该是obj -> unit
类型,这只是一个函数。
请勿将其包裹在EventHandler
中,它会工作:
let dataChangedHandler = fun (sender: obj) -> this.DataItems_DataChanged(sender)