【问题标题】:Navigating to a new page and triggering a WebView导航到新页面并触发 WebView
【发布时间】: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


【解决方案1】:
// load the website in your page's constructor
public partial class Websitemain : ContentPage
{
    private void Websitemain()
    {
      var browser = new WebView();
      browser.Source = "https://google.com";
      Content = browser;
    }
}

【讨论】:

    【解决方案2】:

    但是它只是导航到新页面并且从不触发 webview,所以它只是一个空白屏幕。

    Mainpage.xaml.cs 的代码,您导航到一个新页面 (Website) 而不是您创建的 Websitemain 页面。

    Website.xaml.cs的代码来看,contentpage的名字是Websitemain。你需要保持他们的名字一致。

    修改Mainpage.xaml.cs如下:

    public MainPage()
    {
          InitializeComponent();
    }
    
    private async void OnButtonClickedwebsite(object sender, EventArgs e)
    {
          await Navigation.PushAsync(new Websitemain());
    }
    

    Website.xaml.cs 应更改为 Websitemain.xaml.cs

    public partial class Websitemain : ContentPage
    {
        public class Webview : ContentPage
        {
           private void Websitemain()
           {
              //await Navigation.PushAsync(new Website());
              var browser = new WebView();
              browser.Source = "https://google.com";
              Content = browser;
           }
         }
    }
    

    那么你的代码就可以工作了。

    【讨论】:

    • 感谢您的回复。我做了这些编辑,但它仍然加载一个白页。我很确定这与我的 websitemain.xaml 页面有关。我在上面添加的。
    • @mynameis 您可以展示一个示例项目。我会检查它。 :)
    猜你喜欢
    • 2018-12-22
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多