Welcome Guest Search | Active Topics | Sign In | Register

Migration from System.Windows.Forms.WebBrowser to EO.Webbrowser howto attach events Options
pixafe
Posted: Saturday, June 25, 2022 12:12:01 PM
Rank: Member
Groups: Member

Joined: 6/25/2022
Posts: 10
I am migrating from System.Windows.Forms.WebBrowser to EO.Webbrowser.
My current code uses IHTMLElement2 objects to attach / detach events to / from DOM Elements of the html document which are implemented in c#.

This allows me to call an EventHandler in c# when a event (e.g. "onclick") is fired by a html object in DOM.
See also StackOverflow

Is there a sample which shows how to implement?

Thanks
Alex
eo_support
Posted: Saturday, June 25, 2022 12:20:26 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

You would use eoapi.extInvoke or window.external to call back to the C# side. See here for more details:

https://www.essentialobjects.com/doc/webbrowser/advanced/jsext.html

The code would be something like this:

Code: C#
webView.EvalScript(@"
document.getElementById('button1').onclick = function()
{{
    eoapi.extInvoke('command_name', [command_args]);
}}");


This would attach a JavaScript handler to button1's onclick and calls eoapi.extInvoke when the button is clicked. This would in turn trigger the WebView's JSExtInvoke event on the C# side. You can then do whatever you want to do there.

Hope this helps. Please feel free to let us know if you still have any more questions.

Thanks!
pixafe
Posted: Sunday, June 26, 2022 4:24:30 AM
Rank: Member
Groups: Member

Joined: 6/25/2022
Posts: 10
Thanks for this tip! But it is still not working as expected.
Is WebView.LoadCompleted fired when the DOCUMENT is loaded completed as well or is this (occurs when the page has loaded completely) before this?

I tried this
_browser.WebView.EvalScript(
@"document.getElementById('inlayImage').onclick=function(){{eoapi.extInvoke('LazyLoadImage');}}");

but my c# Method LazyLoadImage is not called.

Thanks
Alex
eo_support
Posted: Sunday, June 26, 2022 2:51:17 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

eoapi.extInvoke does NOT call your function directly. It rasies the WebView's JSExtInvoke event. As such you would need to handle the WebView's JSExtInvoke event, then call whatever function you want to call inside your event handling code.

You can take a look of our TabbedBrowser sample application source code to see how this event is handled.

WebView.LoadCompleted is fired when the document is loaded. It is very much the C# side equvailent of document.onload event on the JavaScript side.

Hope this helps.

Thanks!
pixafe
Posted: Monday, June 27, 2022 6:03:31 AM
Rank: Member
Groups: Member

Joined: 6/25/2022
Posts: 10
This is fine now, thanks a lot.
But now there is a problem using InvokeFunction("setInlayImageSrc", s): this is trowing the exception
Invoking function 'setInlayImageSrc' failed because the function to be called is invalid.
thanks, Alex
eo_support
Posted: Monday, June 27, 2022 7:00:46 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
pixafe wrote:
This is fine now, thanks a lot.
But now there is a problem using InvokeFunction("setInlayImageSrc", s): this is trowing the exception
Invoking function 'setInlayImageSrc' failed because the function to be called is invalid.
thanks, Alex


Please only follow up your question in one thread. We will continue discussion on the other thread.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.