我如何使用MSHTML的的addEventListener在IE9中添加事件侦听器? [英] How do I add an event listener using MSHTML's addEventListener in IE9?

查看:584
本文介绍了我如何使用MSHTML的的addEventListener在IE9中添加事件侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文档 的addEventListener 说,它接受一个回调函数在的IDispatch * 对象的形式。从C#(我使用COM互操作),Visual Studio的显示参数类型为刚对象

The MSDN documentation for addEventListener says it accepts a callback function in the form of an IDispatch * object. From C# (I'm using COM interop), Visual Studio displays the parameter type as just object.

我找了个 IEventListener 接口或类似的东西,但一个也没找到。我到底在传递?

I looked for an IEventListener interface or something similar but didn't find one. What am I supposed to pass in?

推荐答案

经过一番研究,我了解到,这些COM连接点(事件处理程序)指定与 DISPID(0)。回调函数由相同的类的实例来表示:

After some research, I learned that these COM connection points (event handlers) are specified with DispId(0). Callback functions are represented by instances of classes like:

// These attributes may be optional, depending on the project configuration.
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class EventListener
{
    [DispId(0)]
    // The "target" parameter is an implementation detail.
    public void NameDoesNotMatter(object target, IDOMEvent evt) { ... }
}

由于 DISPID(0)指定调用默认的方法,该方法的实际名称并不重要。然而,该方法的参数当然重要。例如, IHTMLElement.onclick 必须分配回调不带任何参数,而 IHTMLElement2.attachEvent 采用了回调一个类型参数 IHTMLEventObj (或 IHTMLEventObj2 ... 6 ,甚至只是对象)。

Since DispId(0) specifies the default method to invoke, the actual name of the method doesn't matter. However, the method parameters certainly do matter. For example, IHTMLElement.onclick must be assigned a callback with no arguments, while IHTMLElement2.attachEvent takes a callback with one parameter of type IHTMLEventObj (or IHTMLEventObj2, ..., 6 , or even just object).

在综上所述,COM 的IDispatch 回调可在C#中使用COM可见类接受正确的参数,并标注有的方法来实现[DISPID(0)]

In summary, COM IDispatch callbacks can be implemented in C# using a COM-visible class with a method that accepts the correct arguments and is annotated with [DispId(0)].

尽管这一切,这避免了W3C DOM事件的API可以是更合适的,因为IE9 DOM对象不支持的解决方案此方法时浏览器使用的兼容性较低的文档模式。例如,扩展,使用的addEventListener 将无法像冰,这是在IE7模式呈现在页面上。

Despite all of this, solutions that avoid the W3C DOM Events API may be more appropriate, as IE9 DOM objects do not support this method when the browser is using a lower document mode for compatibility. For example, an extension that uses addEventListener will fail on a page like Bing, which is rendered in IE7 mode.

这也似乎并不可能设置为从做手工通过F12开发人员工具使用的的IWebBrowser2 实例抛开文档模式。

It also doesn't seem possible to set the document mode used by an IWebBrowser2 instance aside from manually doing it through the F12 developer tools.

这篇关于我如何使用MSHTML的的addEventListener在IE9中添加事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆