如何从webview中检查404错误Xamarin表单
如何使用xamarin表单检查webview上服务中的404错误或任何其他错误? 我的想法不是显示警报,只是显示一个带有一般错误消息的页面,如果服务返回,用户刷新页面后立即重定向到原始页面如何从webview中检查404错误Xamarin表单,webview,xamarin.forms,http-status-code-404,http-redirect,Webview,Xamarin.forms,Http Status Code 404,Http Redirect,如何使用xamarin表单检查webview上服务中的404错误或任何其他错误? 我的想法不是显示警报,只是显示一个带有一般错误消息的页面,如果服务返回,用户刷新页面后立即重定向到原始页面 public myPage1() { InitializeComponent(); WebView.Source = "https://anypageoutthere.com/"; } protected async
public myPage1()
{
InitializeComponent();
WebView.Source = "https://anypageoutthere.com/";
}
protected async override void OnAppearing()
{
base.OnAppearing();
if (!internet.IsSuccess)
{
await Navigation.PushAsync(new 404page());
}
然而,我不知道在(404page.cs)中该怎么做才能在服务不正常的情况下立即刷新并返回(myPage1)。
这是正确的方法吗?如果我有10个不同的网络视图和juts(1)404通用页面,如果没有可用的服务。
欢迎提出任何意见。
谢谢 这可能是一种处理你想要的东西的方法: 通过将所有404错误页面放在单独的XAML文件中来创建一个错误页面 myPage1.xaml:
<ContentPage.Content>
<WebView x:Name="webView"/> <!--Your web view here-->
<userControl:errorPage x:Name="errorPage"/> <!--Your generic error page-->
<Button x:Name="refreshButton" Text="Refresh Page" Clicked="Refresh_OnClicked"/>
</ContentPage.Content>
public myPage()
{
InitAll();
}
protected override async void OnAppearing()
{
base.OnAppearing();
await CheckUrlResponse();
}
private void InitAll()
{
webView.IsVisible = false;
errorPage.IsVisible = false;
}
private async Task CheckUrlResponse()
{
var response = await client.GetAsync("Your Request Url");
var statusCode = response.StatusCode;
if(statusCode == HttpStatusCode.NotFound) //404 error
{
errorPage.IsVisible = true;
webView.IsVisible = false;
}
if(statusCode == HttpStatusCode.OK)
{
errorPage.IsVisible = false;
webView.IsVisible = true;
WebView.Source = "Your Request Url";
}
}
private async void Refresh_OnClicked(object sender, EventArgs e)
{
await CheckUrlResponse();
}
抱歉,无法使用XF WebView检查请求的状态。即使你可以,你描述的场景也有点奇怪,因为404通常不会在几分钟后消失我不知道我是否能做得更好,但这不是双重工作吗?首先请求页面检查状态,然后在设置WebView.Source时再次请求。这可以优化吗?