使用WebBrowser.ObjectForScripting(C#+ IE11)的问题 [英] problem with using WebBrowser.ObjectForScripting (C# + IE11)

查看:263
本文介绍了使用WebBrowser.ObjectForScripting(C#+ IE11)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是
我尝试用我的类来扩展我的WebBrowser控件实例,我想从页面内部通过javascript调用,然后我遇到了问题。这是一个说明我的问题的例子。我有一个有2种方法的课程,如下所示:

Hello,
I try to extend my an instance of WebBrowser control with my class that I'd like to call via javascript from inside the page and I run into theissue. Here is the example that illustrates my problem. I have a class with 2 methods, like this:

  [ComVisibleAttribute(true)]

  public class WebBrowserExtra

  {

   public bool CanDoAction()   {return true;}

   public void DoAction(int objId){InternalDoAction(objId);}

 }

 [ComVisibleAttribute(true)]
 public class WebBrowserExtra
 {
   public bool CanDoAction()   { return true;}
   public void DoAction(int objId) {InternalDoAction(objId);}
 }

我将WebBrowserExtra的实例分配给WebBrowser控件的ObjectForScripting属性。 br />


如果在javascript中我调用window.external.CanDoAction(),一切都很好 - 我甚至可以在"CanDoAction"中加入一个断点。并看到它被调用。但是,只要javascript调用window.external.DoAction(1),就会生成一个错误,该错误读取
"Class不支持自动化"。看来我们在这个问题上没有任何帮助。


I assign an instance of WebBrowserExtra to ObjectForScripting property of WebBrowser control.

If in javascript I call window.external.CanDoAction(), everything is OK - I can even put a break-point inside "CanDoAction" and see it being invoked. However as soon as javascript calls window.external.DoAction(1) an error is generated that reads "Class doesn't support automation". It seems like there is nothing helpful our there on the subject.

任何线索都将非常感谢 - 谢谢。

Any clue would be greatly appreciated - thank you.

软件开发人员

推荐答案

嗨marek_m,

Hi marek_m,

我测试了你的代码,它在我身边很好用。以下是我的示例代码。

I tested your code, it works fine on my side. Below is my sample code.

<html>
    <head>
        <script type="text/javascript">
            function btnClick()
            {
                alert(1);
                window.external.CanDoAction();
                alert(2);
                window.external.DoAction(3);
                alert(4);
            }
        </script>
    </head>
    <body>
        <input type="button" onclick="btnClick()" value="Click Me" />
    </body>
</html>

请确保您已将WebBrowserExtra对象设置为ObjectForScripting 属性 在调用DoAction方法之前。

Please make sure you have asign WebBrowserExtra object to ObjectForScripting  property before you invoke DoAction method.

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.ObjectForScripting = new WebBrowserExtra();
}


最好的问候,b
Li Wang

Best Regards,
Li Wang


这篇关于使用WebBrowser.ObjectForScripting(C#+ IE11)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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