javascript - 我试图在鼠标单击 CEFSharp 中的 Winforms RichTextbox 时获取 html 标记名称,但将 null 作为异常

我试图在鼠标单击时获取 Selected Html 标记元素。我正在使用 CEFSharp。例如,当客户端单击 H1 或 P 标记时,选定的 HTML 标记元素名称将在 Richtextbox 上。

但作为回报得到以下异常

System.NullReferenceException: 'Object reference not set to an instance of an object.'

CefSharp.JavascriptResponse.Result.get returned null.

这是我的 demo.html 文件

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1>Hello World</h1>
<p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the
    industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and
    scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into
    electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of
    Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus
    PageMaker including versions of Lorem Ipsum.
</p>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</body>
</html>

这是我的脚本文件。我正在使用 jquery。

$('*', document.body).click(function (event) {
    event.stopPropagation();
    return dotnetMessage.show(event.target.nodeName);
});

这是我的 winforms C# 代码

public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        public Form1()
        {
            InitializeComponent();
            InitializeChromium();
        }
        
        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            Cef.Initialize(settings);
            chromeBrowser = new ChromiumWebBrowser();
            chromeBrowser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            chromeBrowser.JavascriptObjectRepository.Register("dotnetMessage", new DotNetMessage(),BindingOptions.DefaultBinder);
            // Add it to the form and fill it to the form window.
            this.panel1.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
            chromeBrowser.LoadingStateChanged+= OnLoadingStateChanged;
        }

        private async void OnLoadingStateChanged(object? sender, LoadingStateChangedEventArgs e)
        {
            var scriptJsPath = string.Format(@"{0}\script.js", Application.StartupPath);
            var script = File.ReadAllText(scriptJsPath);
            // var script = @"(function(){return 'HALLOOOO'})();"; // this is working

            var result = await chromeBrowser
                .GetMainFrame().EvaluateScriptAsync(script)
                .ContinueWith(t => t.Result.Result.ToString());
            richTextBox1.Invoke(new Action(() => { richTextBox1.Text = result; }));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String page = string.Format(@"{0}\demo.html", Application.StartupPath);
            chromeBrowser.Load(page);
        }
    }

    public class DotNetMessage
    {
        public string Show(string message)
        {
            return message;
        }
    }

回答1

您的脚本可能会评估为 jQuery 对象,该对象非常复杂,无法解析/表示为 .Net 对象。因此,在这种情况下,预计会返回 null。

应使用标记名称调用 DotNetMessage.Show 方法,您应更新 show 方法以更新richTextBox1.Text 属性。

对于简单的消息传递,我建议使用 CefSharp.PostMessage 而不是绑定对象。 https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#JSEvent有一个例子

相似文章

最新文章