You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are lots of cases when you don't need to explicitly bind an object, you just wish to pass some message data back to the browser. This will be added to every V8Context, so will be immediately available for javascript execution.
Add .Net event
Add CefSharp.PostMessage javascript method.
Add detailed example to this issue
CefSharp.PostMessage(message);
I'm thinking we won't impose a strict message structure, will use the current conversion from Javascript->C# so whatever data types you can transfer now will be supported.
For simple cases there's no need to bind an object to get communication up and running
An easy way to handle events/button clicks etc
Details:
Accepts only a single param, this can be a primitive type like int, string or a complex object
IJavascriptCallback is supported (javascript function pointer)
You can use dynamic in c# for complex objects or use the built in Model Binder via the JavascriptMessageReceivedEventArgs .ConvertMessageTo<T>() method to convert to a complex object
//In Javascript you call CefSharp.PostMessage or cefSharp.PostMessage//Either case is supported//You can post a primitive type like int, string or a complex type like belowCefSharp.PostMessage({"Type": "Update",Data: {"Property": 123},"Callback": responseFunc});
browser.JavascriptMessageReceived+=OnBrowserJavascriptMessageReceived;privatevoidOnBrowserJavascriptMessageReceived(objectsender,JavascriptMessageReceivedEventArgse){//Complext objects are initially expresses as IDicionary//You can use dynamic to access properties (the IDicionary is an ExpandoObject)//dynamic msg = e.Message;//Alternatively you can use the built in Model Binder to convert to a custom modelvarmsg=e.ConvertMessageTo<PostMessageExample>();varcallback=(IJavascriptCallback)msg.Callback;vartype=msg.Type;varproperty=msg.Data.Property;//Call a javascript function with your response datacallback.ExecuteAsync(type);}
I'd suggest you try casting msg.Callback to IJavascriptCallback before attempting to call ExecuteAsync. If that doesn't work then I'd need to see the PostMessage javascript.
Activity
amaitland commentedon Jun 3, 2019
Use Cases:
Details:
int,stringor a complex objectIJavascriptCallbackis supported (javascript function pointer)dynamicinc#for complex objects or use the built inModel Bindervia theJavascriptMessageReceivedEventArgs .ConvertMessageTo<T>()method to convert to a complex objectamaitland commentedon Mar 12, 2020
The How do you handle a Javascript event in C#? of the
FAQhas been updated to usePostMessage.woth2k3 commentedon May 17, 2020
amaitland commentedon May 17, 2020
@woth2k3 Please no images of code, see https://github.com/cefsharp/CefSharp/wiki/Questions-&-Support#no-images-of-code
I'd suggest you try casting
msg.CallbacktoIJavascriptCallbackbefore attempting to callExecuteAsync. If that doesn't work then I'd need to see thePostMessagejavascript.woth2k3 commentedon May 17, 2020
Sorry for the image. I've edited the message and made it as it's supposed to be.
Worked like a charm with casting. Many thanks!
gf21T commentedon Mar 9, 2021
amaitland commentedon Mar 9, 2021
@gf21T You don't appear to be using CefSharp.PostMessage, I'd suggest you ask for help on stackoverflow.
BuslikDrev commentedon Apr 29, 2023
Предлагаю добавить ещё в таком стиле написания функцию "CefSharp.PostMessage" => "CefSharp.postMessage"