【问题标题】:Xamarin Forms - Webview not showing upXamarin 表单 - Webview 未显示
【发布时间】:2015-12-17 09:01:38
【问题描述】:

我正在开发一个小型 Xamarin.Forms webview 应用程序。这是之前回答的问题的后续问题,xamarin-forms-making-webview-go-back

所以我有一个工具栏和一个后退按钮实现和工作。但是当我在模拟器已经打开的情况下运行程序时(我使用 Genymotion),程序运行并显示工具栏以及后退按钮......但不会显示 webview。

但奇怪的是,有时当我在模拟器处于睡眠模式时运行程序然后将其重新打开时,程序运行良好。另外,当我在 iOS 上测试它时,它只显示了工具栏,根本没有 webview!通常情况下,模拟器不会显示 webView。我也在我的 Android 设备上对此进行了测试,并且发生了同样的事情,它会显示工具栏而不是 web 视图。

我知道有点困惑,但任何人都可以帮助我解决这个问题。

我将在下面附上我的代码:

App.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace WebView_form
{
    public class App : Application
    {
        public App()
        {
            //const string URL = "http://www.google.com";
            MainPage = new NavigationPage(new WebPage());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

WebPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;

using Xamarin.Forms;

namespace WebView_form
{
    public class WebPage : ContentPage
    {
        private WebView webView;

        public WebPage()
        {
            webView = new WebView 
            {
                Source = "https://www.google.com"
            };


            // toolbar
            ToolbarItems.Add(new ToolbarItem("Back", null, () =>
                {
                    webView.GoBack();
                }));

            Content = new StackLayout
            {
                Children = { webView }
            };
        }
    }
}

如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin-studio


    【解决方案1】:

    VerticalOptions 设置为FillAndExpand 并为HorizontalOptions 执行相同操作,如果这不起作用。

    可能WebView 的高度为零,因为当布局发生时,视图仍然是空的。

    因此,像这样更改 WebPage.cs 中的代码;

    // ... Other code
    
    public WebPage()
    {
      webView = new WebView 
      {
         Source = "https://www.google.com",
         VerticalOptions = LayoutOptions.FillAndExpand,
         HorizontalOptions = LayoutOptions.FillAndExpand
      };
    
    
      // toolbar
      ToolbarItems.Add(new ToolbarItem("Back", null, () =>
      {
         webView.GoBack();
      }));
    
      Content = new StackLayout
      {
         Children = { webView }
      };
    }
    
    // ... Other code
    

    【讨论】:

      【解决方案2】:

      要考虑的另一件事:如果您正在响应 WebView.Navigating 事件,如果加载的页面是 WevView.Source,请确保不要将其 args.Cancel 设置为 true。 WebView.Navigating 的 iOS 实现在 WebView.Source 加载但 Android 实现不加载时触发。如果在相关页面为 WebView.Source 时将 args.Cancel 设置为 true,则 WebView 将为空白。

      【讨论】:

      • 你能更好地解释一下你的想法吗?
      【解决方案3】:

      将此设置到您的 Info.plist 以绕过应用程序传输安全检查。访问https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity 了解更多信息。

      <key>NSAppTransportSecurity</key>
      <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
      </dict>
      

      注意:我的 URL 已经是 HTTPS,不知道为什么它仍然被阻止。

      【讨论】:

      • 不推荐!不允许非 HTTPS 加载。您很可能使用的是自签名证书,因此它被阻止了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多