Message 1 of 1
Cefsharp browser JavaScript Binding not working
Not applicable
11-25-2020
02:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Forum,
For the current addin I am developing I have implemented a CefSharp browser inside of a DockableDialog loading a local .html page. Currently I am trying to bind a .Net class and call its functions from the Webpage via JavaScript Binding of CefSharp (https://github.com/cefsharp/cefsharp/issues/2246). But I have run into the following issues:
- When trying to bind the object via CefSharp.BindObjectAsync() from the Webpage it won't bind the object at all.
- When trying to bind the object via browser.ExecuteScriptAsync("CefSharp.BindObjectAsync();") it will bind the object but I am unable to call its functions afterwards.
This is the code I have:
- Binding the object in JS and calling the showMessage function on button click:
function CallAddin()
{
(async function()
{
console.log("HERE");
await CefSharp.BindObjectAsync("bound");
bound.showMessage();
})();
}- Resolve the binding of the object and check if successfully bound in C#:
browser = new ChromiumWebBrowser(); browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
{
var repo = e.ObjectRepository;
if (e.ObjectName == "bound")
{
repo.Register("bound", new Translate(), isAsync: true, options: new BindingOptions());
}
};
browser.JavascriptObjectRepository.ObjectBoundInJavascript += (sender, e) =>
{
var name = e.ObjectName;
TaskDialog.Show("Info", "Successfully bound object");
};- The class to bind to the Webpage:
class Translate
{
public void ShowMessage()
{
TaskDialog.Show("Info", "This was called from the Webpage");
}
}I use:
- Revit2020
- CefSharp 65.0.1
- .NET Framework 4.7.2
Cheers