I have a Xamarin Forms ContentPage that has a WebView and an ActivityIndicator. I've wired up event handlers to to the WebView Navigating and Navigated events to activate and de-activate the ActivityIndicator. This all works great on Android. On iOS, the Navigating event fires multiple times as the page loads, but the Navigated event does not fire consistently with it. This often leaves the ActivityIndicator in a perpetually activated state. Has anyone found a fix or workaround that doesn't involve a custom WebView render or is a custom renderer the only way to go? If a custom renderer is required, does anyone have an example that works for this purpose?
Updated: Added the code here as requested.
<?xml version="1.0" encoding="utf-8" ?>
<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>
using FFXPubXam.Helpers;
using FFXPubXam.ViewModels;
using System;
using Xamarin.Forms;
namespace FFXPubXam.Views
{
public partial class WebPage : ContentPage
{
public WebPage(string arg)
{
InitializeComponent();
BindingContext = new WebPageViewModel(arg);
}
protected override void OnAppearing()
{
ToggleActivityIndicator(true);
base.OnAppearing();
}
private void WebViewControl_Navigating(object sender, WebNavigatingEventArgs e)
{
ToggleActivityIndicator(true);
}
private void WebViewControl_Navigated(object sender, WebNavigatedEventArgs e)
{
ToggleActivityIndicator(false);
}
private void ToggleActivityIndicator(bool state)
{
activity.IsRunning = state;
activity.IsEnabled = state;
activity.IsVisible = state;
}
}
}