日期:2014-05-18  浏览次数:20981 次

【200分】求解决webBrowser调用html页面JS
我想用webBrowser向本地的html页面传值,结果出现了奇怪的问题。

代码如下:

下面是本地的html页面

HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>显示</title>
    <script src="jquery-1.5.2.min.js" type="text/javascript"></script> 
    <style type="text/css">
       .BarNow
       {
           font-size:26px;
           color:Blue;
       }
    </style>
    <script language="javascript" type="text/javascript">
       function ShowCode(source) {
          $("#message").html(source);
       }
    </script>
</head>
   <body>
         <div id="message"></div>
  </body>
</html>



用来订阅消息和发送信息的类

C# code

    //订阅者
    public interface Subscriber
    { 
        void ShowMsg(string Msg);
    }
    public static class Message 
    {
        static IList<Subscriber> Subscribers = new List<Subscriber>();

        public static void Add(Subscriber sub)
        {
            Subscribers.Add(sub);
        }
        public static void Delete(Subscriber sub)
        {
            Subscribers.Remove(sub);
        }
        public static void AssignMessage(string Msg)
        {
            foreach (var sub in Subscribers)
            {
                sub.ShowMsg(Msg);
            }
        }
        //调用Send发送消息给订阅者
        public static void Send(string message)
        {
            AssignMessage(message);
        }
    }



订阅消息的窗体
C# code

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Child : Form ,Subscriber
    {
        public Child(string name)
        {
            InitializeComponent();
            test();        
        }
        private void test()
        {
            webBrowser1.Navigate(HtmlPath+"Page.html");
            webBrowser1.ObjectForScripting = this;
            Message.Add(this);//订阅消息
        }
        //显示消息给html
        public void ShowMsg(string Msg)
        { 
            //检查消息是否接收到,得到接收的消息为;这是测试消息!
            Label1.Text = Msg;
            object[] objects = new object[10];
            objects[0] =  "<span class=\"BarNow\">" + Msg + "</span>";
            webBrowser1.Document.InvokeScript("ShowCode", objects);
            //通过订阅者的方式调用出现InvalidCastException异常。提示数字转换异常??
            //通过button调用却是正常显示!!
        }

        private void Child_FormClosing(object sender, FormClosingEventArgs e)
        {
            Message.Delete(this);//取消订阅
            Dispose();
        }
        //用窗体上button直接调用ShowMsg() 消息能正常显示消息
        private void Button1_Click(object sender, EventArgs e)
        {
            //object[] objects = new object[1];
            //objects[0] = "<span class=\"BarNow\">Adflsfjaslnlu</span>";
            //webBrowser1.Document.InvokeScript("ShowCode", objects);
            ShowMsg("这是测试消息!"); //直接用button调用成功。
        }


父窗体的用打开所有子窗体,其中每个子窗体都是一个订阅者
C# code

               foreach (string child in childs)
                {
                    if (!string.IsNullOrEmpty(child))