Xamarin WebView.CanGoBack 和 CanGoForward 在页面加载后始终返回 false [英] Xamarin WebView.CanGoBack and CanGoForward always return false after page loaded

查看:36
本文介绍了Xamarin WebView.CanGoBack 和 CanGoForward 在页面加载后始终返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin.Forms 的新手,如果我没有使用正确的词,请见谅.我创建了一个名为WebBrowser"的解决方案,以创建一个简单的移动浏览器;目前我正在处理通用文件 MainPage.xamlMainPage.xaml.cs,我正在 Android 5.1<上测试代码/strong> 智能手机.我使用 Microsoft Visual Studio 2017 15.5.4.

I'm new at Xamarin.Forms, so excuse me if I don't use the correct words. I created a solution called "WebBrowser" in order to create a simple mobile browser; at the moment I'm working on the common files MainPage.xaml and MainPage.xaml.cs, and I'm testing the code on an Android 5.1 smartphone. I use Microsoft Visual Studio 2017 15.5.4.

MainPage.xaml 中我指定了以下代码

In MainPage.xaml I specified the following code

<Button Text="Back"
        x:Name="backButton"
        IsEnabled="False"
        Clicked="previousPage"/>

<Button Text="Next"
        x:Name="nextButton"
        IsEnabled="False"
        Clicked="nextPage"/>

<WebView x:Name="appWebView"
         Source="https://www.google.com/"
         Navigating="onLoadingPage"
         Navigated="onPageLoaded"/>

所以当页面加载时,方法 onPageLoaded() 被调用.

so when a page is loaded, the method onPageLoaded() is called.

该方法包含以下代码

private void onPageLoaded(object sender, EventArgs e)
{
    if (appWebView.CanGoBack)
        backButton.IsEnabled = true;
    else
        backButton.IsEnabled = false;

    if (appWebView.CanGoForward)
        nextButton.IsEnabled = true;
    else
        nextButton.IsEnabled = false;
}

为了在用户可以/不能转到上一页/下一页时启用/禁用箭头"按钮.

in order to enable/disable the "arrow" buttons when the user can/can't go to the previous/next page.

在这种情况下,appWebView.CanGoBackappWebView.CanGoForward 总是 返回 false,即使它应该是 true.但是,如果我保持按钮处于启用状态并在用户单击其中一个按钮时检查它们的值,则两者都会返回正确的值.

In this situation appWebView.CanGoBack and appWebView.CanGoForward always return false, even when it should be true. But if I keep the buttons enabled and I check their values when the user clicks one of the buttons, both return the correct value.

为什么它在 onPageLoaded() 方法中不起作用?

Why doesn't it work inside onPageLoaded() method?

推荐答案

这不是您所要求的确切答案,但您可以使用 Bindings 资源来做这样的事情.像这样:

It's not the exact answer to what you asked for, but you can use the Bindings resource to do such a thing. Like this:

<Button Text="Back"
        x:Name="backButton"
        IsEnabled="{Binding CanGoBack, Source={Reference appWebView}}"
        Clicked="previousPage"/>

<Button Text="Next"
        x:Name="nextButton"
        IsEnabled="{Binding CanGoFoward, Source={Reference appWebView}}"
        Clicked="nextPage"/>

<WebView x:Name="appWebView"
         Source="https://www.google.com/"
         Navigating="onLoadingPage"/>

更简单.如果您愿意,请查看这篇文章详细了解 Xamarin.Forms 上的绑定.

It's simpler. Take a look at this article if you wanna know more about bindings on Xamarin.Forms.

因此,如果 onPageLoaded 事件处理程序仅存在于此,您可以摆脱它.

Thus you can get rid of the onPageLoaded event handler if it exists only for this.

希望能帮到你.

这篇关于Xamarin WebView.CanGoBack 和 CanGoForward 在页面加载后始终返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆