A. Cefsharp Call JS
CefSharp.WinForms.ChromiumWebBrowser WB;
Mode 1. The Executescriptasync method is used in the same way as the Eval method of JS, and executes asynchronously with no return value.
XXX is the method name of JS
Wb. Executescriptasync ("xxx ()");
Assign ' abc ' to the variable jsvar of JS
Wb. Executescriptasync ("jsvar= ' abc");
Mode 2. The Evaluatescriptasync method is used in the same way as the Eval method of JS, which executes asynchronously with a return value.
Task<cefsharp.javascriptresponse> t = wb. Evaluatescriptasync ("CallTest2 ()");
After waiting for the JS method to execute, get the return value
T.wait ();
T.result is the Cefsharp.javascriptresponse object
T.result.result is an object, the return value of the CallTest2 () method from JS
if (T.result.result! = null)
{
MessageBox.Show (T.result.result.tostring ());
}
Two. JS calls the Cefsharp object
1. Registering a C # object as a JS object
public class JSEvent
{
public string MessageText = string. Empty;
public void Showtest ()
{
MessageBox.Show ("This in c#.\n\r" + MessageText);
}
}
...
CefSharp.WinForms.ChromiumWebBrowser WB;
...
Cefsharp default Registerjsobject (no camelcasejavascriptnames parameter), only lowercase letters start with a property, method name.
Using Registerjsobject with the Camelcasejavascriptnames parameter, passing in false, you can identify the property, the method name, where the capital letter begins.
Wb. Registerjsobject ("Jsobj", New JSEvent (), false);
2. JS calls the registered C # object
Jsobj.messagetext = "Hello";
Jsobj.showtest ();
Cefsharp and JS call each other