Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
C# WebBrowser控件外部脚本不';行不通_C#_Com_Webbrowser Control - Fatal编程技术网

C# WebBrowser控件外部脚本不';行不通

C# WebBrowser控件外部脚本不';行不通,c#,com,webbrowser-control,C#,Com,Webbrowser Control,这是存储在字符串资源中的HTML: <!DOCTYPE html> <html> <head></head> <body> <script> (function(){ window.external.hello() })() </script> </body> </

这是存储在字符串资源中的HTML:

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script>
           (function(){
               window.external.hello()
           })()
        </script>
    </body> 
</html>
这是JSCallbacks.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace JSIE
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]

    public class JSCallbacks
    {
        public void hello() {
            MessageBox.Show("Hello, world!");
        }
    }
}

当我运行它时,它无法访问JavaScript
窗口.external
对象中的
hello()
方法,并给我一个脚本错误消息框。我曾尝试使用
this
作为
对象进行脚本编写,但它也不起作用。

明白了:在执行页面之前,您必须使用DocumentText属性加载HTML。

对于那些不熟悉
对象进行脚本编写的人来说:好吧,这很奇怪。出于某种原因,当我从
标记调用它时,
hello()
函数不在“那里”,但如果我从事件属性(
onclick=”“
等)调用它,它就在那里了。哦,这可能是因为在调用该代码时,DOM还没有初始化。在解析DOM的其余部分之前,您的代码正在运行。您能否在加载表单后捕获DOM的实时输出?(很抱歉,我也不是100%熟悉这个对象,也从来没有用它写过任何东西。假设加载后可以捕获live DOM元素)加载文档的方式很奇怪。在设置ObjectForScript之后,如何设置DocumentText属性?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace JSIE
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]

    public class JSCallbacks
    {
        public void hello() {
            MessageBox.Show("Hello, world!");
        }
    }
}