如何检测使用WebView在Xamarin中成功加载的网页



我正在使用WebView在我的应用程序中加载网页。(使用Xamarin平台实现(

但当网页加载失败时,我想弹出一条消息给用户。(可能是因为没有互联网(

我该怎么做?

如何检测网页是否成功加载?

谢谢。

您可以使用WebView导航事件。

伴随Navigated事件的WebNavigatedEventArgs对象有一个名为Result的属性,该属性使用WebNavigationResult枚举成员描述导航的结果。有效值为CancelFailureSuccessTimeout

<StackLayout>
<WebView x:Name="webView" Source="https://www.google.com" WidthRequest="1000" Navigated="webView_Navigated" HeightRequest="1000"  />
</StackLayout>
private async void webView_Navigated(object sender, WebNavigatedEventArgs e)
{
if (e.Result == WebNavigationResult.Failure)
{
await DisplayAlert("Hi", "connect failure", "ok");
}

}

最新更新