我正在尝试使用Xamarin为我的一个项目构建一个webview应用程序,但我似乎想不出如何让webview元素转到前一个页面,而不是关闭应用程序。
所以我已经想出了如何检测后退按钮并防止它关闭应用程序,但我想让网页返回,如果网页不能返回,就关闭应用程序。
下面是我目前的代码:
using System;
using Xamarin.Forms; 
using Xamarin.Forms.Xaml; 
using Application = Xamarin.Forms.Application; 
namespace myNewApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public class WebPage : ContentPage
    {
        public object _browser { get; private set; }
        protected override bool OnBackButtonPressed()
        {
                base.OnBackButtonPressed();
                return true;
        }
        public WebPage()
        {
            var browser = new Xamarin.Forms.WebView();
            browser.Source = "https://myurl.com";
            Content = browser;
        }
    }
}我已经尝试了几个答案,我找到了这段代码,但它不起作用,因为覆盖不能访问公共WebPage浏览器变量:
if (browser.CanGoBack)
            {
                browser.GoBack();
                return true;
            }
            else
            {
                base.OnBackButtonPressed();
                return true;
            }任何帮助都将不胜感激。
发布于 2018-08-20 08:38:10
您需要将browser设置为类级别变量,以便可以在页面中的任何位置访问它。
public class WebPage : ContentPage
{
    Xamarin.Forms.Webview browser;
    protected override bool OnBackButtonPressed()
    {
        base.OnBackButtonPressed();
        if (browser.CanGoBack)
        {
            browser.GoBack();
            return true;
        }
        else
        {
            base.OnBackButtonPressed();
            return true;
        }
    }
    public WebPage()
    {
        browser = new Xamarin.Forms.WebView();
        browser.Source = "https://myurl.com";
        Content = browser;
    }
}https://stackoverflow.com/questions/51922856
复制相似问题