【问题标题】:Xamarin Forms WebView firing Navigated event multiple timesXamarin Forms WebView 多次触发导航事件
【发布时间】:2020-02-07 18:19:40
【问题描述】:

我有一个带有 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";
    }

【问题讨论】:

    标签: xamarin.forms android-webview


    【解决方案1】:

    这是我的 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,并且在加载页面时它只会触发一次导航。

    【讨论】:

    • 我做了一个和你一样的新项目。我使用的 Xamarin 版本是 4.2.0.709249。当我在 WebV_Navigated 上设置断点时,我看到它仍然被触发了 3 次。
    • 我尝试将 Xamarin.Forms 降级到 4.1.555618 并得到相同的结果。
    • 在 App.xaml.cs 中分配 MainPage = new WebViewPage(); ?
    • 我已将 WebView 添加到 MainPage。在 App.xaml.cs 我有 MainPage = new MainPage();
    猜你喜欢
    • 2018-02-19
    • 2018-07-22
    • 2021-05-28
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2022-01-26
    相关资源
    最近更新 更多