xamarin.forms - Xamarin Forms WebView 多次触发导航事件
问题描述
我有一个带有 WebView 的简单 ContentPage。WebView Source 手动设置为“ https://www.turkishairlines.com/ ”。结果,我看到 Navigating 事件被触发一次,而 Navigated 事件被触发了 3 次。我尝试使用不同的网站并为不同的网站获取不同数量的导航事件。我试图比较触发的导航事件以找出中间事件和最终事件之间的任何区别,但没有成功。当网站真正完全加载时,我需要捕捉一个事件。我怎样才能赶上最终的导航事件?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,并且在加载页面时它只触发一次导航。