简体   繁体   English

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

[英]I am trying to get html tag name on Mouse click into Winforms RichTextbox in CEFSharp but getting null as Exception

I am trying to get the Selected Html tag element on mouse click.我试图在鼠标单击时获取 Selected Html 标记元素。 I am using CEFSharp.我正在使用 CEFSharp。 For example, when a client will click on the H1 or P tag, the selected HTML tag element name will be on richtextbox.例如,当客户端单击 H1 或 P 标记时,选定的 HTML 标记元素名称将在 Richtextbox 上。

But In return getting this bellow exception但作为回报得到以下异常

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

CefSharp.JavascriptResponse.Result.get returned null.

This is my demo.html file这是我的 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>

This is my script file.这是我的脚本文件。 I am using jquery.我正在使用 jquery。

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

And here is my winforms C# code这是我的 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;
        }
    }

Your script would likely evaluate to the jQuery object which is to complex to be parse/represent as a .Net object.您的脚本可能会评估为 jQuery 对象,该对象非常复杂,无法解析/表示为 .Net 对象。 So returning null is expected in this case.因此,在这种情况下,预计会返回 null。

The DotNetMessage.Show method should be called with the tag name, you should update your show method to update the richTextBox1.Text property.应使用标记名称调用 DotNetMessage.Show 方法,您应更新 show 方法以更新richTextBox1.Text 属性。

For simple message passing I'd suggest using CefSharp.PostMessage instead of binding an object.对于简单的消息传递,我建议使用 CefSharp.PostMessage 而不是绑定对象。 There is an example at https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#JSEvent https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#JSEvent有一个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我没有收到任何错误,但是当我尝试单击“表格”时,我的表格类型鼠标没有出现 - I am not Getting Any Errors but When i am trying to click on Form my form Type Mouse does not appear 通过鼠标单击或悬停显示html表标签名称 - showing html table tag name by mouse click or hover 试图获取行数据警报。 为什么我在点击时收到多个警报? - Trying to get row data alert. Why am I getting multiple alerts on click? 当我想使用 JavaScript 从 HTML 中获取字符串时,为什么会得到 Null - Why am I getting Null when I want to get a String from an HTML with JavaScript 更改 cefsharp winforms 中的选择标签选项 - Change select tag option in cefsharp winforms 我试图获得两列名称的第一列和第二列的增量 60。但我在第二列的每一行中都得到 0 - I am trying to get two columns first of name and second of increment by 60. But i am getting 0 in every row of second column 我无法在引导工具提示中单击html锚标记中的链接 - I am not able to click on link in html anchor tag in bootstrap tooltip 尝试查找元素时没有出现此类元素异常 - Getting no such element exception when I am trying to find an element 单击如何获取WMS图层名称。 我以不同的方式尝试过,但没有得到。 我以这种方式有3个wms层 - How to get wms layer name on click. I tried it in different ways but I am not getting. I have 3 wms layers in this way 我正在尝试在 plotyjs 中单击鼠标绘制直线,并将图像加载为背景 - I am trying to draw straight lines on mouse click in plotyjs with image loaded as background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM