1

我正在使用 Web 浏览器控件并尝试使用document.InvokeScript 在 Javascript 中调用函数。

除了我为 Process.Exited调用事件处理程序的部分之外,它在我的代码中的其他任何地方都可以使用。

 //this function is being called from Javascript code.
public void Open()
{

    //****FROM HERE TO......****

    Object[] param = new Object[2];
    param[0] = "play";
    param[1] = "playDisabled";
    webBrowser1.Document.InvokeScript("disable", param);

    param[0] = "playDisabled";
    param[1] = "Playing";
    webBrowser1.Document.InvokeScript("setText", param);

    //****THERE, INVOKESCRIPT WORKS....****

    //there i open exe file...
    Process GameLaunch = new Process();
    GameLaunch.StartInfo.FileName = ExtractPath + gameFile;
    GameLaunch.StartInfo.Arguments = ExtractPath + gameFile;
    GameLaunch.EnableRaisingEvents = true;

    //after its closed i call GameLaunch_Exited method.
    GameLaunch.Exited += new EventHandler(GameLaunch_Exited);
    GameLaunch.Start();

}
void GameLaunch_Exited(object sender, EventArgs e)
{

   InvokeScript("btnReset", "#playDisabled");
}
private object InvokeScript(string name, params object[] args)
{
    //here it fires an error.
    return webBrowser1.Document.InvokeScript(name, args);
}
        private void button1_Click_1(object sender, EventArgs e)
        {
        InvokeScript("btnReset", "#playDisabled");
        string output = "";
        int i = 0;
        foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("a"))
        {
            i++;
            output += i+". Element: id: "+ element.Id + ", inner html: "+ element.InnerHtml + Environment.NewLine;
        }
       MessageBox.Show(output);
    }

我从这段代码中得到的只是:指定的演员表无效。

以下是一些屏幕:

顺便说一句,如果我从按钮单击处理程序调用它,InvokeScript 方法就可以工作。如果从退出处理程序调用,则不会。

我真的很困惑。

在此处输入图像描述

和错误本身:

在此处输入图像描述

对于那些想看我的JS的人。

function btnReset(id){
  $(document).ready(function(){
     $(id).removeClass("disabled");
     $(id).html("Start");
     $(id).attr("id", "play");
 });
 }

编辑: 异常消息:

System.Windows.Forms.dll 中出现“System.InvalidCastException”类型的未处理异常 {“指定的强制转换无效。”}

附加信息:指定的演员表无效。

谢谢你的帮助。

-干杯

-缺口。

4

0 回答 0