【问题标题】:Xamarin Forms WebView Navigated event not firing consistently on iOSXamarin Forms WebView Navigated 事件未在 iOS 上持续触发
【发布时间】:2021-05-28 23:23:02
【问题描述】:

我有一个 Xamarin Forms ContentPage,它有一个 WebView 和一个 ActivityIndi​​cator。我已将事件处理程序连接到 WebView Navigating 和 Navigated 事件以激活和停用 ActivityIndi​​cator。这一切都适用于Android。在 iOS 上,Navigating 事件会在页面加载时触发多次,但 Navigated 事件不会随之触发。这通常会使 ActivityIndi​​cator 处于永久激活状态。有没有人找到不涉及自定义 WebView 渲染的修复或解决方法,或者自定义渲染器是唯一的方法?如果需要自定义渲染器,是否有人有适用于此目的的示例?

更新:按要求在此处添加代码。

<?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;
        }
    }
}

【问题讨论】:

  • 你能显示你现在使用的代码吗?
  • 我编辑了帖子以添加代码。
  • 这里是导航事件未能触发的站点示例。 www.fairfaxcounty.gov/topics/contact-us
  • 我用你的代码测试了一下,结果是触发WebViewControl_Navigated method,但是需要相当长的时间。看来它的负载相对于Android来说需要很长时间。

标签: xamarin.forms webview


【解决方案1】:

我无法让NavigatedNavigating 在Android 上触发,尚未在iOS 上进行测试,但这可能是因为我正在加载本地HTML 文件,所以我的情况有点不同。 ..无论哪种方式,这种解决方法都可能有助于解决这个一般问题,因为它仍然会多次触发Navigating,但在我没有得到任何一个之前。

我需要在代码隐藏中设置 WebView.SourceWebView.NavigatingWebView.Navigated,而不是像这样在 Xaml 中:

public WebPage(string arg) {
    InitializeComponent();
    BindingContext = new WebPageViewModel(arg);
    WebViewControl.Source = (BindingContext as WebPageViewModel).Url;

    WebViewControl.Navigating += WebViewControl_Navigating;
    WebViewControl.Navigated += ContentEditorWebView_Navigated;
}

我不确定发生这种情况的原因,但我认为这是由于 WebView 不是本地的,并且渲染仅在创建设备特定控件时发生。

【讨论】:

    【解决方案2】:

    我发现如果设置为 _blank,Xamarin WebViews 中显示的 HTML 链接的 target 属性可以阻止 Navigating 事件触发。一个简单的如果 hacky 修复就是在这样的代码中删除它们:

    var htmlSource = new HtmlWebViewSource();
    // remove target="_blank" to make Navigating event fire:
    htmlSource.Html = htmlString.Replace("target=\"_blank\"", "").Replace("target='_blank'", "");
    
    webView.Source = htmlSource;
    

    或者你可以从你的 HTML 源代码中删除它们。

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 1970-01-01
      • 2018-02-19
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多