Welcome Guest Search | Active Topics | Sign In | Register

WebView is opening a new thread Options
Akansha
Posted: Tuesday, March 24, 2020 5:48:15 AM
Rank: Newbie
Groups: Member

Joined: 3/21/2020
Posts: 9
Hi Team,

I am using EO browser in WPF application. Hence I have created a user control with EO web view as shown below
<Grid>
<eo:WebControl>
<eo:WebControl.WebView >
<eo:WebView x:Name="webBrowser" BeforeNavigate="webBrowser_BeforeNavigate" >
</eo:WebView>
</eo:WebControl.WebView>
</eo:WebControl>
</Grid>

On the click of a button on Window form, a new dialog is to be opened in which content will be loaded in EO web browser. In the C#, we are passing some HTML content along with dynamic data, and then html content is loaded in the newly created web view using webBrowser.LoadHtml(content); where content is HTML content.
There are few anchor tags in the html content, and when user clicks on this hyperlink, we have to call a C# function which will check if dialog/modal is opened or not using syntax-
System.Windows.Interop.ComponentDispatcher.IsThreadModal;
Even though the modal is opened this returns false.

Also we need to check if mouse button has been clicked and check if(Mouse.Captured == null) {}
But this statement is giving exception for the form on which EO webview has been loaded.
"System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'"
This may be because EO web view is working on a thread other than the application.

Kindly let us know the solution to solve the above problems.

Thanks
eo_support
Posted: Tuesday, March 24, 2020 11:42:03 AM
Rank: Administration
Groups: Administration

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

How do you call your C# function when user clicks a hyperlink in a WebView?

Thanks!
Akansha
Posted: Tuesday, March 24, 2020 6:08:49 PM
Rank: Newbie
Groups: Member

Joined: 3/21/2020
Posts: 9
Hi,

I am using BeforeNavigate event to find if a hyperlink is clicked and then call a function to perform certain actions based on requirements.
In which, 2 requirements are not working for the web view-
1- System.Windows.Interop.ComponentDispatcher.IsThreadModal : this is returning false even though we have loaded the web view in a dialog.
2- if (Mouse.Captured == null) is giving exception system.invalidoperationexception, even though we are setting Mouse.Capture(this) for webview.
I have also send the sample test project to support mail id.
Please let us know the solution as early as possible.

Thanks
eo_support
Posted: Wednesday, March 25, 2020 9:24:43 AM
Rank: Administration
Groups: Administration

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

You can not do this through BeforeNavigate event. BeforeNavigate event is called in a worker thread so you can not do any of those things that you normally do in your main UI thread. Because BeforeNavigate runs in a worker thread, inside that event you can only perform some non-UI related check and return as quickly as you can.

The safest way to do this is with JavaScript. The basic steps are:

1. Write the JavaScript code that searches for all A element in your HTML and attach onclick handler to them;
2. When user click the link, your JavaScript onclick handler is triggered. Inside your JavaScript code you can use eoapi.extInvoke to call into your C# code. This call will be in main UI thread so you will be able to do pretty much everything you normally do;
3. Depending on the return value of your C# side code, you can then decide whether to cancel or proceed with the navigation;

Note that we do not provide tech support on JavaScript code. But intercepting link is common and if you search online, you should be able to find plenty of sample code.

Once you have the correct JavaScript code to do that, you can run the code using webView.EvalScript. You can call this method this way:

Code: C#
//Load the HTML and wait for it to finish
NavigationTask task = webView.LoadHtml(your_html);
task.WaitOne();

//Run your JavaScript code
webView.EvalScript(your_script);


See here for more information on how to call C# code from your JavaScript code:

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

Hope this helps.

Thanks!
PhilipT
Posted: Friday, March 27, 2020 6:06:11 AM
Rank: Advanced Member
Groups: Member

Joined: 2/15/2014
Posts: 52
HI Eo

What is the recommended approach when loading an HTML file? Should the application wait for the LoadCompleted event or use NavigationTask to determine if the HTML page and it's javascript dependency files are fully loaded?

I was told by a web developer (I'm not a web developer), that loading HTML page doesn't necessarily mean all the javascript files are loaded yet so if that is the case, is there a better way of ensuring that the HTML page is really fully loaded and ready? I thought of putting a Sleep(5000) when the LoadCompleted event arrives.

Regards
eo_support
Posted: Friday, March 27, 2020 8:09:26 AM
Rank: Administration
Groups: Administration

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

You can use either NavigationTask or LoadCompleted event. When a page is loaded, the following are true:

1. The main page has finished loading and has been fully parsed;
2. All synchronous (default) JavaScript files/code have been loaded and executed;

Other things are NOT loaded, these mainly includes:

1. Dependency resources such as images or CSS;
2. async or deferred JavaScript files/code;

Many web developers uses async/deferred JavaScript to speed up page loading and there is no standard way to determine whether they have been loaded or not.

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.