【发布时间】:2019-04-09 22:11:52
【问题描述】:
单击按钮后使用 Xamarin 在另一个页面上打开 Web 视图。
我正在尝试使用 Xamarin 构建一个跨平台应用程序(更新于 4-03-19)。在应用程序的主页上,我有几个按钮。通过其中一个按钮,我希望使用 webview 来加载网站。如果我使用其中一个按钮从该主页启动 webview,我可以完成。但是,在该主页上,我将导航设置为 false,这样我就看不到该页面上的导航。这就是问题发生的地方。因为导航设置为 false,一旦调用 web 视图就无法返回。我的解决方案是让我的按钮导航到新页面。然后我试图在页面加载后触发 webview。但我无法做到这一点。
Mainpage.xaml
NavigationPage.HasNavigationBar="False"
Mainpage.xaml.cs
public MainPage()
{
InitializeComponent();
}
private async void OnButtonClickedwebsite(object sender, EventArgs e)
{
await Navigation.PushAsync(new Website());
}
//private void OnButtonClickedwebsite(object sender, EventArgs e)
//{
// var browser = new WebView();
// browser.Source = "https://google.com";
// Content = browser;
//}
网站.xaml
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:test"
x:Class="test.Websitemain"
BackgroundColor="White">
<ContentPage.Content>
<StackLayout>
<WebView x:Name="websitemain" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
网站.xaml.cs
public partial class Websitemain : ContentPage
{
public class Webview : ContentPage
{
private void Websitemain(EventArgs e)
{
//await Navigation.PushAsync(new Website());
var browser = new WebView();
browser.Source = "https://google.com";
Content = browser;
}
}
按钮应该导航到新页面,然后启动我的 webview。但是它只是导航到新页面,从不触发 webview,所以它只是一个空白屏幕。
【问题讨论】:
-
您正在推送页面
Website,但正在显示页面Websitemain的代码以及另一个Webview的内部类??? -
下面的答案会对你有所帮助,只需在 AndroidManifest 文件中添加互联网访问权限即可。
标签: xamarin xamarin.forms