[英]How to trap/listen javascript function or events in cefsharp
I am trying to use cefSharp for a WPF application.我正在尝试将 cefSharp 用于 WPF 应用程序。 I can find "how to call a Javascript methods from .Net . But is there a way where I can get notified for Javascript functions or events in .Net?
我可以找到“如何从 .Net 调用 Javascript 方法。但是有没有一种方法可以让我在 .Net 中获得 Javascript 函数或事件的通知?
eg if there is a Javascript function (with and without param) I can get the notification with or without values in .Net.例如,如果有一个 Javascript 函数(带和不带参数),我可以在 .Net 中收到带或不带值的通知。
In short, YES!简而言之,是的! There's bindings available for:
有可用的绑定:
c# -> js use c# -> js 使用
webBrowser.ExecuteScriptAsync(script);
webBrowser.ExecuteScriptAsync(script);
js -> c# use js -> c# 使用
webBrowser.RegisterJsObject(A_NAME_FROM_JS, objectToBind);
webBrowser.RegisterJsObject(A_NAME_FROM_JS, objectToBind);
Then from js you can call:然后从 js 你可以调用:
window.A_NAME_FROM_JS.MethodOnObj()
window.A_NAME_FROM_JS.MethodOnObj()
and this will invoke the这将调用
MethodOnObj()
MethodOnObj()
on the passed在通过
objectToBind
对象绑定
It works like a charm.它就像一个魅力。 Let me know if you've an issue and I'll help further!
如果您有问题,请告诉我,我会进一步提供帮助!
The RegisterJsObject
and ExecuteScriptAsync
methods are both deprecated now. RegisterJsObject
和ExecuteScriptAsync
方法现在都已弃用。 I found another solution that CefSharp
supports here:我在这里找到了
CefSharp
支持的另一个解决方案:
https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#JSEvent https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#JSEvent
More detailed information is here: https://github.com/cefsharp/CefSharp/issues/2775#issuecomment-498454221更详细的信息在这里: https : //github.com/cefsharp/CefSharp/issues/2775#issuecomment-498454221
Essentially, from your javascript
code on page, you can call:本质上,从页面上的
javascript
代码中,您可以调用:
CefSharp.PostMessage(someDataYouWantToPassBack)
And in C#
, you register the event to your browser:在
C#
,您将事件注册到浏览器:
browser.浏览器。 JavascriptMessageReceived += OnBrowserJavascriptMessageReceived;
JavascriptMessageReceived += OnBrowserJavascriptMessageReceived;
private void OnBrowserJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.