MSHTML:调用JavaScript对象的成员吗? [英] MSHTML : Calling member of javascript object?

查看:352
本文介绍了MSHTML:调用JavaScript对象的成员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET WebBrowser控件,它是相当简单的执行一个HTMLElement的一员。

Using the .NET WebBrowser control, it is fairly simple to execute a member of an HtmlElement.

假设有一个名为运动员与一个名为getLastSongPlayed成员的JavaScript对象;从.NET WebBrowser控件调用这个会去是这样的:

Assuming there is a JavaScript object called "player" with a member called "getLastSongPlayed"; calling this from the .NET WebBrowser control would go something like this:

HtmlElement elem = webBrowser1.Document.getElementById("player");
elem.InvokeMember("getLastSongPlayed");

现在我的问题是:我如何完成,使用MSHTML

Now my question is: How do I accomplish that using mshtml ?

在此先感谢,
Aldin

Thanks in advance, Aldin

编辑:

我得到它运行起来,见下文!我的答案

I got it up and running, see my answer below !

推荐答案

最后!我得到它运行起来!

FINALLY !! I got it up and running !

究其原因

System.InvalidCastException

这被抛出,每当我试图引用parentWindow的mshtml.IHTMLDocument2和/或指定给一个mshtml.IHTMLWindow2窗口对象曾与线程做。

that was thrown, whenever I tried to reference the parentWindow of an mshtml.IHTMLDocument2 and / or assign it to an mshtml.IHTMLWindow2 window object had to do with Threading.

对于一些不明给我,理由似乎mshtml.IHTMLWindow的COM对象在另一个线程必须是单线程单元经营(STA)的状态。

For some, unknown to me, reason it seems that the COM objects of mshtml.IHTMLWindow are operating on another Thread that must be of Single-Threaded Apartment (STA) state.

因此​​,关键是,调用/执行所需的片code对与STA状态另一个线程。

So the trick was, calling / executing the required piece of code on another Thread with STA state.

下面是一个示例code:

Here's a sample code:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer

bool _isRunning = false;

private void IE_DocumentComplete(object pDisp, ref obj URL)
{
    //Prevent multiple Thread creations because the DocumentComplete event fires for each frame in an HTML-Document
    if (_isRunning) { return; }

    _isRunning = true;

    Thread t = new Thread(new ThreadStart(Do))
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
}

private void Do()
{
    mshtml.IHTMLDocument3 doc = this.IE.Document;

    mshtml.IHTMLElement player = doc.getElementById("player");

    if (player != null)
    {
        //Now we're able to call the objects properties, function (members)
        object value = player.GetType().InvokeMember("getLastSongPlayed", System.Reflection.BindingFlags.InvokeMethod, null, player, null);

        //Do something with the returned value in the "value" object above.
    }
}

我们现在正在还能够引用一个mshtml.IHTMLDocument2对象的parentWindow并执行脚本的网站和/或我们自己的(请记住,必须是一个STA线程):

We're now also able to reference the parentWindow of an mshtml.IHTMLDocument2 object and execute a sites script and / or our own (remember it must be on an STA thread):

mshtml.IHTMLWindow2 window = doc.parentWindow;

window.execScript("AScriptFunctionOrOurOwnScriptCode();", "javascript");

这可能会节省在将来头痛的人。笑

This might save someone from headaches in the future. lol

这篇关于MSHTML:调用JavaScript对象的成员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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