WinForms:设置ObjectForScripting时,不显示HTML

WinForms: When ObjectForScripting is set, HTML is not displayed
2020-10-22
  •  译文(汉语)
  •  原文(英语)

我有一个Windows窗体应用程序和一个单独的HTML页面.两者应该互相通信.我按照此Microsoft链接中的说明进行操作.在使用设置ObjectForScripting之前WebBrowser.ObjectForScripting = this;,HTML页面正确显示.但是,设置之后ObjectForScripting,HTML将不会显示在WebBrowser组件中.它给出错误"window.external为null或不是对象".

C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirstCSharpApp
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WebBrowser.AllowWebBrowserDrop = false;
            WebBrowser.IsWebBrowserContextMenuEnabled = false;
            WebBrowser.WebBrowserShortcutsEnabled = false;
            WebBrowser.ObjectForScripting = this;

            string curDir = Directory.GetCurrentDirectory();
            WebBrowser.Navigate(new Uri(String.Format("file:///{0}/Timeline.html", curDir)));
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            // Works!
            WebBrowser.Document.InvokeScript("test",
                new String[] { "called from client code" });
        }

        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser.GoHome();
        }

        private void goBackToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            WebBrowser.GoBack();
        }

        private void goForwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser.GoForward();
        }

        public void Test(String message)
        {
            // Does not work, or apparently even exist
            MessageBox.Show(message, "client code");
        }
    }
}

HTML:

<html>
<head>
    <script>
        function test(message) {
            alert(message);
        }
    </script>
</head>

<body>
    <button onclick ="window.external.Test('called from script')">
        Call client code.
    </button>
</body>
</html>
速聊1:
您确实应该包括您的代码,以防万一出现复制粘贴错误.这是最常见的错误之一.
解决过程1

看起来您在表单类上缺少这些属性.

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

您可以将此属性设置为任何其COM属性可见的公共属性和方法可用于脚本代码的对象.您可以通过用标记COM,使类可见ComVisibleAttribute.

速聊1:
根据我对文档的理解,如果public已经将其标记为Com Visible.我仍然标记了它,但它没有解决我的问题.

I have a Windows forms application and a separate HTML page. The two are supposed to communicate with each other. I followed the instructions from this microsoft link. Before I set the ObjectForScripting with WebBrowser.ObjectForScripting = this;, the HTML page displayed correctly. However, after setting ObjectForScripting, the HTML will not display in the WebBrowser component whatsoever. It gives the error "window.external is null or not an object."

C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirstCSharpApp
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WebBrowser.AllowWebBrowserDrop = false;
            WebBrowser.IsWebBrowserContextMenuEnabled = false;
            WebBrowser.WebBrowserShortcutsEnabled = false;
            WebBrowser.ObjectForScripting = this;

            string curDir = Directory.GetCurrentDirectory();
            WebBrowser.Navigate(new Uri(String.Format("file:///{0}/Timeline.html", curDir)));
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            // Works!
            WebBrowser.Document.InvokeScript("test",
                new String[] { "called from client code" });
        }

        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser.GoHome();
        }

        private void goBackToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            WebBrowser.GoBack();
        }

        private void goForwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WebBrowser.GoForward();
        }

        public void Test(String message)
        {
            // Does not work, or apparently even exist
            MessageBox.Show(message, "client code");
        }
    }
}

HTML:

<html>
<head>
    <script>
        function test(message) {
            alert(message);
        }
    </script>
</head>

<body>
    <button onclick ="window.external.Test('called from script')">
        Call client code.
    </button>
</body>
</html>
Talk1:
You really should include YOUR code, just in case there was a copy paste mistake. It is one of the most common errors.
Solutions1

It looks like you are missing these attributes on your form class.

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

You can set this property to any COM-visible object for which you want its public properties and methods available to scripting code. You can make a class COM-visible by marking it with the ComVisibleAttribute.

Talk1:
From my understanding on the docs, if it is public it will already be tagged as Com Visible. I tagged it anyway, and it did not fix my problem.
转载于:https://stackoverflow.com/questions/30576586/winforms-when-objectforscripting-is-set-html-is-not-displayed

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入