0
votes

I have a simple ContentPage with WebView. WebView Source is manually set to "https://www.turkishairlines.com/". As a result, I see that Navigating event is fired once while Navigated event is fired three times. I tried with different web sites and getting different numbers of Navigated events for different sites. I tried to compare fired Navigated events to find any difference between intermediate and final event but with no success. I need to catch an event when a web site is really loaded completely. How can I catch the final Navigated event? MainPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="WebTest.MainPage">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Label HorizontalOptions="Center" x:Name="NavCounterLabel"/>
    <WebView x:Name="WebV"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand"
                Grid.Row="1"/>
    <ActivityIndicator x:Name="BusyIndicator"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        Color="Red"
                        HeightRequest="50"
                        WidthRequest="50"
                        Grid.Row="1"/>
</Grid>
</ContentPage>

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BusyIndicator.IsRunning = BusyIndicator.IsVisible = true;
        WebV.Source = "https://www.turkishairlines.com/";
        WebV.Navigating += WebV_Navigating;
        WebV.Navigated += WebV_Navigated;
    }

    private void WebV_Navigated(object sender, WebNavigatedEventArgs e)
    {
        BusyIndicator.IsRunning = BusyIndicator.IsVisible = false;
        NavCounterLabel.Text = (Int32.Parse(NavCounterLabel.Text)+1).ToString();
    }

    private void WebV_Navigating(object sender, WebNavigatingEventArgs e)
    {
        BusyIndicator.IsRunning = BusyIndicator.IsVisible = true;
        NavCounterLabel.Text = "0";
    }
1

1 Answers

0
votes

Here is my WebViewPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="IntentoAssociates.WebViewPage">
    <Grid>
        <!-- Place new controls here -->
        <WebView x:Name="WebV"
                 HorizontalOptions="FillAndExpand"
                 VerticalOptions="FillAndExpand" />
        <ActivityIndicator x:Name="BusyIndicator"
                           HorizontalOptions="Center"
                           VerticalOptions="Center"
                           Color="Red"
                           HeightRequest="50"
                           WidthRequest="50" />
    </Grid>
</ContentPage>

And here is my WebViewPage.xaml.cs

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace IntentoAssociates
{
    public partial class WebViewPage : ContentPage
    {
        public WebViewPage()
        {
            InitializeComponent();
            Title = "Intento Associates";
            BusyIndicator.IsRunning = BusyIndicator.IsVisible = true;
            WebV.Source = "https://www.turkishairlines.com/";
            WebV.Navigating += WebV_Navigating;
            WebV.Navigated += WebV_Navigated;
        }

        private void WebV_Navigated(object sender, WebNavigatedEventArgs e)
        {
            BusyIndicator.IsRunning = BusyIndicator.IsVisible = false;
        }

        private void WebV_Navigating(object sender, WebNavigatingEventArgs e)
        {
            BusyIndicator.IsRunning = BusyIndicator.IsVisible = true;
        }
    }
}

I am using Xamarin Forms 4.1.555618 in all my projects and it fires Navigated just once when the Page is loaded.