委托使用by参数拒绝.NET事件

时间:2014-07-31 16:10:22

标签: .net internet-explorer f#

这让我很难过。

我已经阅读了关于这个主题的SO帖子:here是最相关的,但它并没有涵盖将代表传递给一个事件(尽管我是这样做的)我们预计它会很简单。)

具体来说,VS中的错误是:

This function value is being used to construct a delegate type whose signature includes a byref argument. You must use an explicit lambda expression taking 2 arguments.

我能找到的错误的最佳推理是Eric Lippert's blog中的旁边,但我认为我已经在下面处理过。

// Documentation: http://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx    
// Don't forget to unwrap the instance before using it.
let mutable ieInstance : option<InternetExplorer> = None

let onDocumentComplete (pDisp : Object) (url : byref<Object>) =
    let doc = ieInstance.Value.Document :?> IHTMLDocument2
    let window = doc.parentWindow
    window.execScript(@"alert('Message added by addon.');") |> ignore

// All of the following underline "DocumentComplete" as the source of the error.
do ieInstance.Value.DocumentComplete.AddHandler(new Handler<byref<Object>>(fun pDisp url -> onDocumentComplete pDisp &url))
do ieInstance.Value.DocumentComplete.AddHandler(fun pDisp url -> onDocumentComplete pDisp &url)
do ieInstance.Value.DocumentComplete.Add(fun pDisp url -> onDocumentComplete pDisp &url)
do ieInstance.Value.DocumentComplete.Add(fun _ _ -> ())
do ieInstance.Value.DocumentComplete.Add(fun (_, _) -> ())

我感谢任何建议!

更新1 我引用的库是Interop.SHDocVw,在Microsoft Internet Controls&#39;中。我也尝试使用SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler作为委托类型,但仍然失败。

1 个答案:

答案 0 :(得分:6)

我得到的错误略有不同:

error FS1091: The event 'DocumentComplete' has a non-standard type. If this event is declared in another CLI language, you may need to access this event using the explicit add_DocumentComplete and remove_DocumentComplete methods for the event. If this event is declared in F#, make the type of the event an instantiation of either 'IDelegateEvent<_>' or 'IEvent<_,_>'.

如果你遵循这个建议,你就会得到这个,为我编译:

ieInstance.Value.add_DocumentComplete(fun pDisp url -> onDocumentComplete pDisp &url)

修改

Keith和Tahir的评论让我意识到 - 这种情况确实非常难以诊断,实际上是在F#3.1.1发布后不久的2月修复过。

新行为是更好的错误消息(如上所示)和更好的智能感知条目(现在显示了所需的add_remove_成员,实际上现在隐藏了无用的DocumentComplete条目)。所以如果你使用发布的F#位,抱歉。如果您使用的是最新的预发布F#位,那就不那么痛苦了。

在Codeplex上所有内容都变成开源之前进行了更改,但是在later merge to Github中仍然可以看到它的一些记录(请参阅对infos.fs的更改以及对nameres.fs的最后更改)。