I'm looking for a way to run my Angular2
application in a CefSharp
control and establish a communication in both directions. Let me give you two examples:
C# code:
private void RegisterJsObjects()
{
_browser.RegisterJsObject("cefTestClass", new TestClass());
}
class TestClass
{
public void csF(string message)
{
MessageBox.Show(message, "#C", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
}
Angular code:
<button (click) ="cefTestClass.csF('text')"> C# Button </button>
Angular
functions from my CefSharp
application, because they get different names after compiling, I can't get access to them.The registered object is bound to the window object of javascript
. You missed out the word window
at the invocation. You have to call it like this:
<button (click) ="appComponentFunction()"> C# Button </button>
and in your AppComponent:
appComponentFunction(){
window['cefTestClass'].csF('text');
}
It is important that the methods of your registered object begins with a lower case letter, otherwise javascript
doesn't execute them.
If you want to call a Angular function from c# you can register a callback in your c#-class like this:
private IJavascriptCallback callback;
public void registerCallback(IJavascriptCallback callback)
{
this.callback = callback;
}
now you must define your callback in your Angular app (eg. in the NgOnInit in your root-component):
window['cefTestClass'].registerCallback(function() {
window.alert("Hallo");
});
now you can call the function any time in c#
:
callback.ExecuteAsync();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With