我正在使用WebView在我的应用程序中加载网页。(使用Xamarin平台实现(
但当网页加载失败时,我想弹出一条消息给用户。(可能是因为没有互联网(
我该怎么做?
如何检测网页是否成功加载?
谢谢。
您可以使用WebView导航事件。
伴随Navigated
事件的WebNavigatedEventArgs
对象有一个名为Result
的属性,该属性使用WebNavigationResult
枚举成员描述导航的结果。有效值为Cancel
、Failure
、Success
和Timeout
。
<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");
}
}