Cefsharp and JS call each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.