Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf WebBrowser元素进入和离开事件停止工作_Wpf_Browser - Fatal编程技术网

wpf WebBrowser元素进入和离开事件停止工作

wpf WebBrowser元素进入和离开事件停止工作,wpf,browser,Wpf,Browser,我需要突出显示html中的一些span元素,并显示一个弹出窗口。下面的代码进行了突出显示,但enter和leave事件在使用一段短时间后停止工作(可能有20个enter和leave)。如果我添加弹出窗口,事件将很快停止工作-通常在一个或两个调用之后。我不知道如何调试它,因为没有异常,事件只是停止被调用 我从文档中编写了这段代码,因为我找不到任何人使用如下元素事件处理程序:htmlelementevents2onmouseenterventhandler 注意:此代码需要“使用mshtml;”和对

我需要突出显示html中的一些span元素,并显示一个弹出窗口。下面的代码进行了突出显示,但enter和leave事件在使用一段短时间后停止工作(可能有20个enter和leave)。如果我添加弹出窗口,事件将很快停止工作-通常在一个或两个调用之后。我不知道如何调试它,因为没有异常,事件只是停止被调用

我从文档中编写了这段代码,因为我找不到任何人使用如下元素事件处理程序:htmlelementevents2onmouseenterventhandler

注意:此代码需要“使用mshtml;”和对COM“Microsoft HTML对象库”的引用

 void mBrowser_LoadCompleted(object sender, EventArgs e)
    {
        Debug.WriteLine("LoadCompleted");
        var doc = (IHTMLDocument2)mBrowser.Document;            
        foreach (IHTMLElement elem in doc.all)
        {
            if (elem.tagName == "SPAN")
            {
                mshtml.HTMLElementEvents2_Event iEvt;
                iEvt = (HTMLElementEvents2_Event)elem;
                iEvt.onmouseenter += new HTMLElementEvents2_onmouseenterEventHandler(onMouseEnter);
                iEvt.onmouseleave += new HTMLElementEvents2_onmouseleaveEventHandler(onMouseLeave);
                //iEvt.onmouseover  += new HTMLElementEvents2_onmouseoverEventHandler(onMouseOver);
                Debug.WriteLine("span: " + elem.innerHTML);
            }
        }
    }

    void onMouseEnter(mshtml.IHTMLEventObj e)
    {

        Debug.WriteLine(" mouse enter! " + e.srcElement.innerHTML);
        //mPopup.IsOpen = true;
        //mPopup.HorizontalOffset = e.x;
        //mPopup.VerticalOffset = e.y;
        e.srcElement.style.backgroundColor = "#CCCCCC";

    }
    void onMouseLeave(mshtml.IHTMLEventObj e)
    {
        //mPopup.IsOpen = false;
        Debug.WriteLine(" mouse leave! " + e.srcElement.innerHTML);
        e.srcElement.style.backgroundColor = "#FFFFFF";
    }

您需要保留一个元素事件列表,以避免垃圾收集

List<mshtml.HTMLElementEvents2_Event> mEvts = new List<HTMLElementEvents2_Event>();
static mshtml.HTMLElementEvents2_Event iEvt;

您需要将元素事件保持在静态字段上,以避免垃圾回收

List<mshtml.HTMLElementEvents2_Event> mEvts = new List<HTMLElementEvents2_Event>();
static mshtml.HTMLElementEvents2_Event iEvt;

垃圾被收集起来了吗?我假设在上面的代码中,我正在修改webbrowser文档属性中的元素,因此不应出现任何问题。。