首页 > 解决方案 > 使用 Xamarin.Forms Webview 检测 URL 更改并将当前 URL 与原始 URL 进行比较

问题描述

所以我正在创建一个使用 Xamarin.Forms Webview 的应用程序。我正在尝试检测 URL 何时更改,如果更改,则将原始 URL 与当前 URL 进行比较,然后根据情况显示或隐藏按钮。按钮的目的是返回上一页并继续前进,直到到达其原始目的地。我只希望当用户不在主屏幕上时显示此“返回”按钮。否则,总是显示。

我已经用 if(webview.cangoback...) 尝试了一切,但是没有检测到 url 的变化。我尝试设置一个等于原始 URL 的字符串并使用 .Equals 比较 webview.source (这是我目前所在的位置)

我刚开始研究 webviewNavigating 但仍然没有。

namespace Webview_Test
{
    public partial class MainPage : ContentPage
    {
        public static string CurrentUrl { get; set; }
        public MainPage()
        {

            InitializeComponent();

            string CurrentUrl = "https://www.google.com/";

            var _webView = new WebView()
            {
                Source = "https://www.google.com/",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            Button BackButton = new Button
            {
                Text = "Go Back",
                BackgroundColor = Color.FromHex("990000"),
                TextColor = Color.White
            };
            BackButton.Clicked += OnBackButtonClicked;

            void OnBackButtonClicked(object sender, EventArgs e)
            {
                _webView.GoBack();
            }


            Grid grid = new Grid
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                RowDefinitions =
                {
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                    new RowDefinition { Height = new GridLength(50, GridUnitType.Absolute) },
                    new RowDefinition { Height = new GridLength(15, GridUnitType.Absolute) },
                    new RowDefinition { Height = new GridLength(15, GridUnitType.Absolute) },
                    new RowDefinition { Height = new GridLength(36, GridUnitType.Absolute) }
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = GridLength.Auto },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(50, GridUnitType.Absolute) },
                    new ColumnDefinition { Width = new GridLength(50, GridUnitType.Absolute) },
                    new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                    new ColumnDefinition { Width = GridLength.Auto }
                }
            };

            grid.Children.Add(_webView, 0, 6, 0, 7);

            if (_webView.Source.Equals(CurrentUrl))
            {
                grid.Children.Remove(BackButton);
            }
            else
            {
                grid.Children.Add(BackButton, 2, 4, 4, 6);
            }

            Content = grid;
        }
    }
}

我的预期结果是主页上没有显示“返回”按钮。但在主页以外的任何页面上,它都应该显示“返回”按钮。从逻辑上讲,如果 OriginalURL = CurrentURL 不显示按钮。如果 OriginalURL != CurrentURL 显示按钮。

标签: c#xamarin.formswebview

解决方案


您是否尝试过导航事件?我将在下面举一些例子。

public string OriginalURL = "https://www.stackoverflow.com"
private async void Webview_Navigating(object sender, WebNavigatingEventArgs e)
{
   if(e.Url != OriginalURL)
   { 
      //Write code, show the button or use if(webview.CanGoBack){//your code}
   }
}

推荐阅读