Right way to implement a Xamarin Forms webview whose height adjusts dynamically based on its content

mac121mac121 Member ✭✭✭

I have implemented a Xamarin Webview control, but its height is not adjusting dynamically according to its content. I tried implementing a custom renderer suggested by @nevalenny (https://forums.xamarin.com/discussion/25967/setting-height-of-webview-dynamically) and @MichaelRumpler (https://forums.xamarin.com/discussion/99789/height-of-a-webview) The first one by @nevalenny was fine, but it is not working always, it cuts some portion of the content with incorrect height (@MichaelRumpler's ViewTreeObserver.predraw was not working for me, don't why). The important thing is that I have images also in my HTML source string. So, maybe this is causing the height to be calculated incorrectly as they are loaded. Please suggest me a working solution that works at all times, that is, webview whose height adjusts dynamically based on its content.

I also tried to evaluate javascript "document.body.offsetHeight", but I'm not able to set the returned value to webview Height. I don't know how to set webview height before webview is displayed, sometimes the problem is retrieving value from the script result when I followed @AdamPedley (https://xamarinhelp.com/xamarin-forms-webview-executing-javascript/)

NOTE: I am not binding a URL as source for the webview, I am binding the HTML string as my webview source (using HtmlWebviewsource)

Any working solution is appreciated, thanks in advance.

Answers

Sign In or Register to comment.