question

SibalikMike-8717 avatar image
0 Votes"
SibalikMike-8717 asked JessieZhang-2116 edited

Xamarin.iOS Can’t control iOS Activity Indicator from WebView Navigating and Navigated events

I have a ContentPage that has a WebView and an ActivityIndicator. There are event handlers for the Navigating and Navigated events to activate and de-activate the ActivityIndicator. On iOS, the Navigating event fires, but the Navigated event does not fire consistently. This often leaves the ActivityIndicator in a perpetually activated state. This can be reproduced by navigating to https://www.fairfaxcounty.gov/topics/contact-us.

It was suggested that I use a custom web renderer to respond to DidFinishNavigation and DidFailNavigation and send a MessagingCenter message to turn off the activity indicator. I added this and it (mostly) worked, but for whatever reason (possibly the update to XF 5 or change under the covers to WKWebView), it has stopped working. DidFinishNavigation still fires in the custom renderer, but now Navigating and Navigated never fire on the WebView. DidFailNavigation never fires and the pages are rendering completely and correctly, so I don’t believe failed navigation is the issue. If I remove the custom renderer, it’s back to the Navigating event firing and Navigated only firing some of the time.

I would greatly appreciate any insight on this.

 < ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
                 xmlns: x = "http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns: vm = "clr-namespace:FFXPubXam.ViewModels"
                 x: Class = "FFXPubXam.Views.WebPage"
                 x: Name = "root"
                 Shell.NavBarIsVisible = "false" >
     < ContentPage.Content >
         < Grid >
             < WebView x: Name = "WebViewControl"
                         Source = "{Binding Url}"
                         Navigating = "WebViewControl_Navigating"
                         Navigated = "WebViewControl_Navigated" />
             < ActivityIndicator x: Name = "activity"
                                 IsRunning = "False"
                                 IsEnabled = "False"
                                 IsVisible = "False"
                                 HeightRequest = "40"
                                 WidthRequest = "100"
                                 VerticalOptions = "CenterAndExpand"
                                 HorizontalOptions = "CenterAndExpand"
                                 Color = "{DynamicResource FFX_Blue4}"
                                 BackgroundColor = "Transparent”/>
             </ Grid >
     </ ContentPage.Content >
 </ ContentPage >
    
 [QueryProperty("Url", "url")]
 [XamlCompilation(XamlCompilationOptions.Compile)]
 public partial class WebPage : ContentPage
 {
     private string baseUrl = string.Empty;
     private bool subscribed = false;
    
     private string url;
    
     public string Url
     {
         set
         {
             if (value != null)
             {
                 url = Uri.UnescapeDataString(value);
             }
         }
         get
         {
             return url;
         }
     }
    
     public WebPage()
     {
         InitializeComponent();
         BindingContext = new WebPageViewModel();
     }
    
     public WebPage(string arg)
     {
         Url = arg;
    
         InitializeComponent();
         BindingContext = new WebPageViewModel();
     }
    
     protected override void OnAppearing()
     {
         if (baseUrl == string.Empty)
         {
             if (Url != null)
             {
                 baseUrl = Url;
             }
         }
    
         if (WebViewControl.Source?.ToString() != baseUrl)
         {
             WebViewControl.Source = baseUrl;
         }
    
         base.OnAppearing();
    
         Logging.Write(LogType.Info, $"OnAppearing: Url={url}");
    
         if (!subscribed) SubScribe();
    
         ToggleActivityIndicator(true);
     }
    
     private void WebViewControl_Navigating(object sender, WebNavigatingEventArgs e)
     {
         Logging.Write(LogType.Info, $"Navigating: Url={url}");
         ToggleActivityIndicator(true);
     }
    
     private void WebViewControl_Navigated(object sender, WebNavigatedEventArgs e)
     {
         Logging.Write(LogType.Info, $"Navigated: Url={url}");
         ToggleActivityIndicator(false);
     }
    
     private void ToggleActivityIndicator(bool state)
     {
         Logging.Write(LogType.Info, $"ToggleActivityIndicator: state={state} stack={new StackTrace()}");
         activity.IsRunning = state;
         activity.IsEnabled = state;
         activity.IsVisible = state;
     }
    
     private void SubScribe()
     {
         Logging.Write(LogType.Info, $"Subscribe: call stack={new StackTrace()}");
    
         MessagingCenter.Subscribe<object>(this, "End", (sender) =>
         {
             ToggleActivityIndicator(false);
         });
     }
 }
        
 [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
 namespace FFXPubXam.iOS.Renderers
 {
     class MyDelegate : WKNavigationDelegate
     {
         public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
         {
             MessagingCenter.Send<object>(this, "End");
             Logging.Write(LogType.Info, $"DidFinishNavigation: after Send");
         }
         public override void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
         {
             MessagingCenter.Send<object, string>(this, "End", error.ToString());
         }
     }
    
     class CustomWebViewRenderer : WkWebViewRenderer
     {
         protected override void OnElementChanged(VisualElementChangedEventArgs e)
         {
             base.OnElementChanged(e);
             if (e.NewElement != null)
             {
                 this.NavigationDelegate = new MyDelegate();
             }
         }
     }
 }


dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

but for whatever reason (possibly the update to XF 5 or change under the covers to WKWebView)

Did this issue come up before you changed your app?

In addition, could you please post the code of WebPageViewModel?
0 Votes 0 ·

0 Answers