Hello,
You can using Handler to customize the WebView. You may refer to Customize controls with handlers.
And implement it in code behind,
public DailySaintPage()
{
InitializeComponent();
ModifyWebView();
...
CallDailySaint(data);
}
void ModifyWebView()
{
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if ANDROID
if (handler.PlatformView is global::Android.Webkit.WebView platformWebView)
{
platformWebView.SetWebViewClient(new CustomWebViewClient(this.android_webview));
}
#endif
});
}
And here is the code for CustomWebViewClient class,
#if ANDROID
public class CustomWebViewClient : WebViewClient
{
Microsoft.Maui.Controls.WebView mywebview;
public CustomWebViewClient(Microsoft.Maui.Controls.WebView webView)
{
mywebview = webView;
}
public async override void OnPageFinished(Android.Webkit.WebView view, string url)
{
mywebview.HeightRequest = 0;
await Task.Delay(500); // Allow time for content to load
string js = "document.body.scrollHeight";
var height = await mywebview.EvaluateJavaScriptAsync(js);
if (double.TryParse(height, out double newHeight) && newHeight > 0)
{
mywebview.HeightRequest = newHeight; // Set WebView height dynamically
}
base.OnPageFinished(view, url);
}
}
#endif
And you don't need custom renderer any more.
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.