[英]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.