简体   繁体   中英

Create and trigger custom event using the WPF webbrowser control

It's relatively straightforward to create and fire a custom event in JavaScript. I'm trying to do the same in the WPF WebBrowser control. My naive approach:

    private void FirePreviewUpdateEvent()
    {
        var doc4 = (IHTMLDocument4) WPFBrowser.Document;
        if (doc4 == null) return;
        object eventObject = doc4.CreateEventObject(null);
        doc4.FireEvent("onpreviewupdated", ref eventObject);
    }

eventObject returns a valid ComObject , but firing the event throws an ArgumentException with a message that an argument is out of range (doesn't say which argument). What have I done wrong?

Well, it took more than a few hours, but I found this works:

private void FirePreviewUpdateEvent()
{
    dynamic doc = Browser.Document;
    var ev = doc.createEvent("event");
    ev.initEvent("previewUpdated", true, true);
    doc.dispatchEvent(ev);
}

I'm holding out something that utilizes the published IHtmlDocument interfaces since methods like initEvent are deprecated. If anyone has insight on this please do share.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM