【发布时间】:2019-10-10 09:19:05
【问题描述】:
所以我正在创建一个使用 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 不显示按钮。 if OriginalURL != CurrentURL 显示按钮。
【问题讨论】:
-
每次新页面开始加载时都会触发导航事件 - 您尝试过吗?
标签: c# xamarin.forms webview