A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Thanks for reaching out.
I have tested a simple .NET MAUI 9 app that demonstrates how to properly load a URL with custom headers in a WebView on Android, without getting a blank white screen. You can refer this code example below.
File Android/Handler/CustomWebViewHandler.cs
#if ANDROID
using Android.Webkit;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
namespace CustomWebViewDemo;
public partial class CustomWebViewHandler : WebViewHandler
{
protected override void ConnectHandler(Android.Webkit.WebView platformView)
{
base.ConnectHandler(platformView);
platformView.Settings.JavaScriptEnabled = true;
platformView.Settings.DomStorageEnabled = true;
var customView = VirtualView as CustomWebView;
var headers = customView?.Headers ?? new Dictionary<string, string>();
platformView.SetWebViewClient(new CustomWebViewClient(this, headers));
}
}
public class CustomWebViewClient : MauiWebViewClient
{
private readonly Dictionary<string, string> _headers;
public CustomWebViewClient(WebViewHandler handler, Dictionary<string, string> headers)
: base(handler)
{
_headers = headers;
}
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
var url = request.Url?.ToString();
if (_headers.Count > 0)
{
view.LoadUrl(url, _headers);
}
else
{
view.LoadUrl(url);
}
return true;
}
}
#endif
File CustomWebView.cs (in root)
namespace CustomWebViewDemo
{
public class CustomWebView : WebView
{
public static readonly BindableProperty HeadersProperty = BindableProperty.Create(nameof(Headers), typeof(Dictionary<string, string>), typeof(CustomWebView));
public Dictionary<string, string> Headers
{
get => (Dictionary<string, string>)GetValue(HeadersProperty);
set => SetValue(HeadersProperty, value);
}
}
}
Hope this helps. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance provide feedback.
Thank you.