Search code examples
c#.netwebviewmaui

WebView Navigated Event Not Triggered in .NET MAUI


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:

  1. The Source property of the WebView is set to a valid URL.
  2. I have subscribed to the Navigated event both in XAML and C#.
  3. Despite this, the Navigated event handler is not invoked after the navigation is completed. 4.Navigating event is getting triggered as expected

I 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!


Solution

  • 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));
    }