I'm wondering what the current situation with trying to get WebViews to resize dynamically in Xamarin forms is?
I've been trawling through some of the posts, but the main issue is that a lot of the solutions seem to be out of date, and no longer work, or will take an unreasonable amount of work to get running.
The situation I'm having like many other people is I have a WebView (inside a grid, inside a stacklayout, rendering HTML) and I need it to resize dynamically.
The documentation says
It may be necessary to set the WidthRequest and HeightRequest properties of the WebView to see the HTML content, depending upon the layout the WebView is a child of. For example, this is required in a StackLayout.
If I don't explicitly set the height and width requests, the HTML doesn't render. That is basically forcing me to pick a static height. I think the width might be ok, but I'd have to double check.
A lot of the proposed solutions are to do with custom renders:
https://github.com/xamarin/Xamarin.Forms/issues/1711
These were the two most promising solutions I found. There are also options where you call java script code but apparently that runs into some serious performance issues, so I'd like to avoid that where possible.
The problem I ran into when trying to implement the above solutions were that that they didn't compile because the dependencies were broken. Xamarin has a bad habit of renaming or moving things, and I think this is one of those cases.
For example, when I try the following code, I get an error:
'WebView' does not contain a constructor that takes 0 arguments
using System.Text;
using System;
using System.Threading.Tasks;
using Android.Content;
using Android.Webkit;
using MyProject.Android.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using WebView = Android.Webkit.WebView;
[assembly: ExportRenderer(typeof(ExtendedWebView), typeof(ExtendedWebViewRenderer))]
namespace MyProject.Android.Renderers
{
public class ExtendedWebViewRenderer : WebViewRenderer
{
public static int _webViewHeight;
static ExtendedWebView _xwebView = null;
WebView _webView;
public ExtendedWebViewRenderer(Context context) : base(context)
{
}
class ExtendedWebViewClient : WebViewClient
{
WebView _webView;
public async override void OnPageFinished(WebView view, string url)
{
try
{
_webView = view;
if (_xwebView != null)
{
view.Settings.JavaScriptEnabled = true;
await Task.Delay(100);
string result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()");
_xwebView.HeightRequest = Convert.ToDouble(result);
}
base.OnPageFinished(view, url);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
_xwebView = e.NewElement as ExtendedWebView;
_webView = Control;
if (e.OldElement == null)
{
_webView.SetWebViewClient(new ExtendedWebViewClient());
}
}
}
public class ExtendedWebView : WebView
{
}
}
My guess is this is something to do to the changes xamarin did to WebView when Apple stopped supporting UIWebView in favour of WKWebView. (you read about this here).
So my overall questions is, has anyone had similar issues and what is the current solution?