【发布时间】:2021-05-28 23:23:02
【问题描述】:
我有一个 Xamarin Forms ContentPage,它有一个 WebView 和一个 ActivityIndicator。我已将事件处理程序连接到 WebView Navigating 和 Navigated 事件以激活和停用 ActivityIndicator。这一切都适用于Android。在 iOS 上,Navigating 事件会在页面加载时触发多次,但 Navigated 事件不会随之触发。这通常会使 ActivityIndicator 处于永久激活状态。有没有人找到不涉及自定义 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