I am experiencing an issue with the WebView
control in my .NET MAUI app where the Navigated
event does not fire after navigation.
Here’s the scenario:
WebView
is set to a valid URL.Navigated
event both in XAML and C#.Navigated
event handler is not invoked after the navigation is completed.
4.Navigating
event is getting triggered as expectedI am implementing an activity indicator while the web view is loading the page. The activity indicator is made visible when Navigating event is triggered but never goes off even after page is loaded because Navigated event is not getting triggered.
Environment:
.NET MAUI Project targeting Android, iOS, and Windows
Android 34.0, iOS, and Windows 10.0.19041.0 platform versions
Troubleshooting Done:
Verified that the Navigated event is subscribed properly.
Ensured the URL is valid and accessible via a regular browser.
Confirmed platform-specific configurations:
Android: INTERNET permission is present in AndroidManifest.xml.
Why is the Navigated event not being triggered, and how can I resolve this issue? Any insights or solutions would be greatly appreciated!
When you set a custom AndroidWebViewClient using
platformView.SetWebViewClient(new AndroidWebViewClient(this));
it replaces the default WebView client, which is responsible for triggering the Navigated event.
You can modify your AndroidWebViewClient to explicitly trigger the Navigated event in the onPageFinished method.
private class AndroidWebViewClient : Android.Webkit.WebViewClient
{
private readonly WebViewConfigurationService service;
private readonly WebView webView;
public AndroidWebViewClient(WebViewConfigurationService service, WebView webView)
{
this.service = service;
this.webView = webView;
}
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
var navigatedEventArgs = new WebNavigatedEventArgs(
WebNavigationEvent.NewPage,
url,
null,
WebNavigationResult.Success
);
// Manually invoke the Navigated event
if (webView != null)
{
var navigatedEvent = webView.GetType().GetField(
nameof(WebView.Navigated),
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
if (navigatedEvent != null)
{
var eventDelegate =
(MulticastDelegate)navigatedEvent.GetValue(webView);
if (eventDelegate != null)
{
foreach (var handler in eventDelegate.GetInvocationList())
{
handler.Method.Invoke(handler.Target, new object[] { webView, navigatedEventArgs });
}
}
}
}
}
And while setting webview client pass the Maui WebView
if (webView.Handler?.PlatformView is Android.Webkit.WebView platformView)
{
platformView.SetWebViewClient(new AndroidWebViewClient(this, webView));
}