Xamarin Forms Web View 多次触发导航事件



我有一个简单的带有WebView的内容页面。Web 视图源手动设置为"https://www.turkishairlines.com/"。结果,我看到导航事件被触发一次,而导航事件被触发三次。我尝试使用不同的网站,并为不同的网站获取不同数量的导航事件。我试图比较触发的导航事件,以找出中间事件和最终事件之间的任何差异,但没有成功。 当网站真正完全加载时,我需要捕获一个事件。如何捕捉最终的导航事件? 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";
}

这是我的 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>

这是我的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;
}
}
}

我在我的所有项目中都使用 Xamarin Forms 4.1.555618,并且在加载页面时它只触发一次导航。

最新更新