c# - Xamarin Forms webview 未在设备上显示

标签 c# android ios webview xamarin

过去几天我在 Xamarin.Forms 中开发了一个 webview 应用程序,我一直在 android 和 iOS 模拟器上测试它,它似乎在模拟器中工作得很好但是当我去尝试自己测试它时Android 设备,它只显示了 xamarin 启动画面(我目前使用的是试用版),然后只是过渡到空白屏幕而不是 webview。

有没有人知道为什么要这样做?

我将在下面附上我的代码: App.cs

using Xamarin.Forms;

namespace WebViewApp
{
    public class App() : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = 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 Xamarin.Forms;

namespace WebViewApp
{
    public class WebPage : ContentPage
    {
        private const string URL = "https://www.google.com";
        private const int PADDING_WIDTH = 0;

        private int paddingHeight;

        private WebView webView;

        public WebPage()
        {
            webView = new WebView
            {
                Source = URL,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            CheckDevice();

            Content = new StackLayout
            {
                Padding = new Thickness(PADDING_WIDTH, GetPaddingHeight()),
                Chrildren = { webView }
            };
        }

        public int GetPaddingHeight()
        {
            return paddingHeight;
        }

        /// <summary>
        /// This will set the padding height for the webview when displayed
        /// <summary>
        /// <param name="pHeight">Set integer value for the padding height.</param>
        public void SetPaddingHeight(int pHeight)
        {
            paddingHeight = pHeight;
        }

        private void CheckDevice()
        {
            if(Device.OS == TargetPlatform.Android)
            {
                SetPaddingHeight(0);
            }
            else if(Device.OS == TargetPlatform.iOS)
            {
                SetPaddingHeight(20);
            }
        }
    }
}

**更新** 我正在使用公司网站,但我一直在使用许多不同的网站(如谷歌、YouTube 和亚马逊)测试此应用程序。似乎唯一不会在我的设备上显示的网站是我公司的网站(它是一个响应式网站),但所有其他网站都会显示。

最佳答案

自版本 9 起,iOS 将只允许您的应用程序与默认实现最佳安全实践的服务器通信。必须在 Info.plist 中设置值以启用与不安全服务器的通信。

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

关于c# - Xamarin Forms webview 未在设备上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414423/

相关文章:

c# - 是什么 | (管道)在 C# 中是什么意思?

c# - 触摸相对于屏幕的位置。 Windows手机

c# - 编译一个 xpath 表达式 - 有一个无效的标记

ios - 快速配置深层链接?

ios - 当 iOS 应用程序即将被 iTunes 或 App Store 更新时在运行时得到通知?

c# - web api 2 ExceptionHandlerFilter OnExceptionAsync 未触发

java - 从android中的linikedList中删除项目

java - 确定设备存储空间不工作 : android

java - 如何在Java类中存储UUID字段?

ios - NSDictionary,如何存储和读取枚举值?