看官方文档比中文百度效果好太多,使用cefsharp强烈建议看官方文档,FAQ中可能就有你的问题。
最近做的一个WPF项目,嵌入cefsharp控件加载html,实现百度地图显示和交互,在地图上利用百度地图api添加了Marker,我需要点击marker执行C#的事件,marker的监听事件是JS事件,cefsharp执行事件使用函数ExecuteJavaScriptAsync,获取事件返回值使用函数EvaluateScriptAsync,可当点击marker发生的JS事件是通过事件监听实现的,而JS中addEventListener是没有返回值的,故cefsharp采用了message进行交互,html中代码如下
var point = new BMapGL.Point(longtitude, latitude);
var myIcon = new BMapGL.Icon(url, new BMapGL.Size(10, 15));
var marker = new BMapGL.Marker(point, {
icon: myIcon
});
marker.setTitle(id);
// 将标注添加到地图
var infoWindow = new BMapGL.InfoWindow(id, point);
map.addOverlay(marker);
marker.addEventListener('click', clickListener);
function clickListener(e) {
if (e.target instanceof BMapGL.Marker) {
CefSharp.PostMessage(e.target.getTitle());
}
}
我们只需要在每次事件发生时向cefsharp发送message就能是实现C#后台对事件的监听
C#部分代码,browser是cefsharp浏览器对象,已经进行过初始化,这两个事件会对浏览器加载和获取message进行监听
browser.JavascriptMessageReceived += OnBrowserJavascriptMessageReceived;
browser.FrameLoadEnd += OnFrameLoadEnd;
public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
//执行Js的监听事件
newMap.ExecuteScriptAsync("clickListener();");
}
}
//C#接收html发送的消息
private void OnBrowserJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
var windowSelection = e.Message.ToString();
//拿到消息说明执行了Js事件并发过来消息,在下面做要处理的事情即可
//DO SOMETHING WITH THIS MESSAGE
//This event is called on the threads pool, to access your UI thread
//You can cast sender to ChromiumWebBrowser
//use Control.BeginInvoke/Dispatcher.BeginInvoke
}
https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions
官网第13个问题
- How do you handle a Javascript event in C#?
For basic communication you can use CefSharp.PostMessage(message); in Javascript to send a message to .Net which triggers the browser.JavascriptMessageReceived event.
// After your ChromiumWebBrowser instance has been instantiated (for WPF directly after `InitializeComponent();` in the control constructor).
// Subscribe to the following events
browser.JavascriptMessageReceived += OnBrowserJavascriptMessageReceived;
browser.FrameLoadEnd += OnFrameLoadEnd;
public void OnFrameLoadEnd (object sender, FrameLoadEndEventArgs e)
{
if(e.Frame.IsMain)
{
//In the main frame we inject some javascript that's run on mouseUp
//You can hook any javascript event you like.
browser.ExecuteScriptAsync(@"
document.body.onmouseup = function()
{
//CefSharp.PostMessage can be used to communicate between the browser
//and .Net, in this case we pass a simple string,
//complex objects are supported, passing a reference to Javascript methods
//is also supported.
//See https://github.com/cefsharp/CefSharp/issues/2775#issuecomment-498454221 for details
CefSharp.PostMessage(window.getSelection().toString());
}
");
}
}
private void OnBrowserJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
var windowSelection = (string)e.Message;
//DO SOMETHING WITH THIS MESSAGE
//This event is called on the threads pool, to access your UI thread
//You can cast sender to ChromiumWebBrowser
//use Control.BeginInvoke/Dispatcher.BeginInvoke
}