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

dan*_*n p 3 .net internet-explorer f#

这让我很难过.

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

具体来说,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.
Run Code Online (Sandbox Code Playgroud)

我能找到的错误的最佳推理是在Eric Lippert的博客中,但我认为我已经在下面处理了它.

// 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 (_, _) -> ())
Run Code Online (Sandbox Code Playgroud)

我很感激任何建议!

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

lat*_*kin 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<_,_>'.
Run Code Online (Sandbox Code Playgroud)

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

ieInstance.Value.add_DocumentComplete(fun pDisp url -> onDocumentComplete pDisp &url)
Run Code Online (Sandbox Code Playgroud)

编辑

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

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

在Codeplex上所有内容都变成开源之前进行了更改,但在稍后与Github的合并中仍然可以看到它的一些记录(请参阅更改为infos.fs并最后更改为nameres.fs).