WebBrowser控件onpropertychange事件的事件处理程序-发送者和e对象为null [英] event handler for WebBrowser control onpropertychange events - sender and e objects are null

查看:72
本文介绍了WebBrowser控件onpropertychange事件的事件处理程序-发送者和e对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我正在服务器端线程中运行WebBrowser(WB)控件,并希望监视(监听)"onpropertychange"事件.我可以成功附加一个.NET委托签名方法,该方法在属性更改时执行,但是在每次调用qEventHndlr时 sender和e对象都为空,因此,我不知道哪个属性已更改触发事件.el是一个HTMLElement,它在foreach循环中进行了迭代,以将事件处理程序附加到每个要监视(监听)的元素上.

In C#, I am running the WebBrowser (WB) control in a server-side thread and want to monitor (listen) for "onpropertychange" events. I can successfully attach a .NET delegate signature method that is executed when a property changes, but the sender and e objects are both null in every call to qEventHndlr, therefore, I don't know which property changed to fire the event. el is an HTMLElement that is iterated in a foreach loop to attach the eventhandler to each element to monitor (listen).

el.AttachEventHandler("onpropertychange", qEventHndlr);     // in the foreach loop
public void qEventHndlr(Object sender, EventArgs e) {…}     // the event handler

文档指出EventArgs类;此类不包含任何事件数据;引发事件时不会将状态信息传递给事件处理程序的事件使用该类.如果事件处理程序需要状态信息,则应用程序必须从此类派生一个类来保存数据."似乎(我不确定并且尚未测试)发件人仅填充了一部分WB事件,而不填充了onpropertychange.

The documentation indicates that the EventArgs class; "This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data." It also seems (I'm not certain and haven't tested) that sender is only populated for a subset of WB events and not onpropertychange.

因此,我当时以为,如果无法在send对象或e对象中获取标识符信息,则可以在AttachEventHandler中使用匿名方法,然后将唯一的运行时可编程字符串参数传递给事件处理程序中嵌入的方法.方法调用.

So, I then thought that if I can't get identifier information in the send or e objects, I could use an anonymous method in the AttachEventHandler and then pass a unique runtime programmable string parameter to a method embedded in the event handler method call.

el.AttachEventHandler("onpropertychange", delegate(Object sender, EventArgs e) { anonoMeth(elID); });
public void anonoMeth(string specificProperty) {..}

编译器接受语法,但是,即使elID字符串在foreach循环中发生更改,也仅使用第一个迭代值,以便在每个onpropertychange事件中调用anonoMeth(string specificProperty)时,specificProperty具有相同的值,因为相同的elID已附加到各个元素.

The compiler accepted the syntax, however, even though the elID string changes in the foreach loop, only the first iteration value is used so that when anonoMeth(string specificProperty) is called with each onpropertychange event, specificProperty has the same value because the same elID was attached to the respective element.

我还没有尝试过扩展方法,所以想把它发布出来,看看是否有人遇到过类似的挑战(并希望有一个解决方案).除非绝对必要,否则我不愿求助于C ++.

I haven't tried Extension Methods yet and wanted to get this posted to see if anyone has encountered similar challenges (and hopefully has a solution). I prefer not to resort to C++ unless I absolutely have to.

推荐答案

您需要将 sender 强制转换为适当类型的对象(本例中为 HTMLElement )在事件处理程序中.

You need to cast your sender to an object of the appropriate type (an HTMLElement in your case) in your event handler.

尝试以下示例以了解尺寸:

Try this example on for size:

namespace WebBrowserEventTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            webBrowserTest.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserTest_DocumentCompleted);
            webBrowserTest.Navigate("http://mondotees.com");
        }

        private void webBrowserTest_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach(HtmlElement el in webBrowserTest.Document.GetElementsByTagName("div"))
            {
                el.AttachEventHandler("onpropertychange", delegate { testEventHandler(el, EventArgs.Empty); });
            }

            foreach (HtmlElement el in webBrowserTest.Document.GetElementsByTagName("div"))
            {
                el.Name = "test";
            }
        }

        public void testEventHandler(object sender, EventArgs e)
        {
            var he = (HtmlElement)sender;
        }
    }
}

这篇关于WebBrowser控件onpropertychange事件的事件处理程序-发送者和e对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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