Xamarin Forms webview 未显示在设备上 [英] Xamarin Forms webview not displaying on device

查看:27
本文介绍了Xamarin Forms webview 未显示在设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ive developing a webview app in Xamarin.Forms over the last few days and ive been testing it on an android and iOS emulator and it seems to work just fine in the emulators but when i went and tried to test it on my own Android device, it just showed the xamarin splashscreen(im using the trial version at the moment) and then just transitioned to a blank white screen instead of the webview.

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

Does anyone have any ideas why it is doing this?

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

I will attach my code below: 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);
            }
        }
    }
}

** 更新 **我正在使用公司网站,但我一直在使用许多不同的网站(例如 google、youtube 和 amazon)测试此应用程序.似乎唯一不会在我的设备上显示的网站是我的公司网站(它是一个响应式网站),但所有其他网站都这样做.

** UPDATE ** I am using a company website but I have been testing this app out with a number of different sites such as google, youtube, and amazon. It would seem that the only site that wont display on my device is my companies website(its a responsive website) but all of the others do.

推荐答案

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

Since version 9, iOS will only allow your application to communicate with servers that implement best-practice security by default. Values must be set in Info.plist to enable communication with insecure servers.

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

这篇关于Xamarin Forms webview 未显示在设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆