break7533
break7533

Reputation: 148

Trying to call C# code with a JavaScript button on a WebView

Note that I'm specifically talking about calling C# after clicking on a Javascript button that is inside my webview, in this case a HybridWebView.

I followed a lot of tips and ideas I seen around like the samples provided by XLabs and the documentation about HybridWebViews in the Xamarin.Forms guides.

I have the following structure:

Hybridwebview Class with the methods to RegisterAction and InvokeAction;

Page that uses my HybridWebView;

A custom renderer in each of my platforms that are injecting the Javascript I want in the webview.

I believe the problem is in the InjectJS method that I have in my renderer. It is using LoadUrl to inject javascript in the WebView and that is "deleting" my WebView and replacing it with the button that has the Javascript function.

This is the code of my renderer for Android! This is the code of my renderer for Android

On the left is the result that this creates and on the right the result I was expecting On the left is the result that this creates and on the right the result I was expecting

Upvotes: 2

Views: 1976

Answers (2)

Joe Healy
Joe Healy

Reputation: 5817

Setting height and width on a the result saved me.

The stock HybridWebView sample at https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/hybridwebview/ had the "no show" issue.

If I ran to emulator it ran fine.

If it ran to a physical device nothing would show up. Changing index.html

<p id="result">Result: </p> 

to

<div id="result" style="height:400px;width:400px;">Result:</div>

made the output show up.

Upvotes: 1

break7533
break7533

Reputation: 148

The problem was the div I created started without defined values for width and height and because of that the content was being displayed has hidden.

Fixed after adding Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true); and then debugging on the device and google chrome tools

Upvotes: 2

Related Questions