简体   繁体   English

Xamarin WebView通常什么都不显示

[英]Xamarin WebView usually shows nothing

public partial class Page : ContentPage
{
    public Page(string filename)
    {
        if(...
        {
            WebView view = new WebView
            {
                Source = "http://xamarin.com"
                //Source = "http://www.google.com"
                /*Source = new HtmlWebViewSource
                {
                    Html = @"<html><body>
                            <h1>Test Code</h1>
                            <p>The code is working.</p>
                            </body></html>"
                }*/
            };
            Content = new StackLayout()
            {
                Children =
                {
                    view
                }
            };
        }
        else ...

The page appears blank most of the time. 该页面大部分时间显示为空白。 In the xamarin.com example, after a long delay, the page fills in about 20% of the times I run it. 在xamarin.com示例中,经过长时间的延迟,该页面填充了我运行该页面大约20%的时间。 The google and custom tests don't work at all. Google和自定义测试根本不起作用。 The custom one is what I want working for some particular cases that will be easier to create as web code. 自定义代码是我要针对某些特定情况工作的内容,这些情况将更易于创建为Web代码。

In my other cases, the other view components I create and add work perfectly fine. 在其他情况下,我创建和添加的其他视图组件也可以正常工作。 (Buttons, Labels, etc...) (按钮,标签等)

Breakpoints don't show me much. 断点并没有显示太多。 The watchlist is uselessly unable to tell me anything. 该关注列表毫无作用,无法告诉我任何事情。 I can see from stepping through that the right code paths are reached in my various test cases, but that's about it. 通过逐步了解,我可以在各种测试用例中找到正确的代码路径,仅此而已。

Tests are done on my Android phone in the Xamarin Live Player (Android 6.0 - API 23). 在Xamarin Live Player(Android 6.0-API 23)的Android手机上进行了测试。 The code is in Visual Studio 2015. Target platforms are both Android and iOS. 该代码在Visual Studio 2015中。目标平台是Android和iOS。

The corresponding xaml page doesn't have much to it. 相应的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"
             x:Class="Project.Views.Page"
</ContentPage>

EDIT: 编辑:

AndroidManifext.xml has the INTERNET permission: AndroidManifext.xml具有INTERNET权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Company.Project" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="Project.Android"></application>
</manifest>

The page appears blank most of the time. 该页面大部分时间显示为空白。 In the xamarin.com example, after a long delay, the page fills in about 20% of the times I run it. 在xamarin.com示例中,经过长时间的延迟,该页面填充了我运行该页面大约20%的时间。 The google and custom tests don't work at all. Google和自定义测试根本不起作用。 The custom one is what I want working for some particular cases that will be easier to create as web code. 自定义代码是我要针对某些特定情况工作的内容,这些情况将更易于创建为Web代码。

I've tested it and reproduced the problem. 我已经测试过并重现了问题。 This only happens when you are creating webview programmatically in constructor or in OnAppearing . 仅当您在构造函数或OnAppearing编程方式创建OnAppearing时,才会发生这种情况。 I set breakpoint and found that the webview's height doesn't get automatically expanded (always 0). 我设置了断点,发现webview的高度不会自动扩展(始终为0)。 My guess is if you create webview in page's constructor or OnAppearing , webview never get re-measured according to it's web content. 我的猜测是,如果您在页面的构造函数或OnAppearing创建webview,则永远不会根据其Web内容重新测量webview。

So there are two workarounds: 因此,有两种解决方法:

  1. Use webview in Xaml: 在Xaml中使用webview:

     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" ...> <WebView Source="http://xamarin.com"></WebView> </ContentPage> 
  2. Give a HeightRequest in webview's OnNavigated : 举一个HeightRequest中的WebView OnNavigated

     protected override void OnAppearing() { base.OnAppearing(); view = new WebView(); Content = new StackLayout { Children = { view } }; view.Navigated += View_Navigated; view.Source = "http://xamarin.com"; } private void View_Navigated(object sender, WebNavigatedEventArgs e) { (sender as WebView).HeightRequest = Height; } 

Update: If you are using HtmlWebViewSource for your webview's Source. 更新:如果您将HtmlWebViewSource用于HtmlWebViewSource的Source。 View_Navigated won't get triggered. View_Navigated不会被触发。 So in such circumsance, you can set webview.HeightRequest explicitly after setting webview.Source : 因此,在这样的circumsance,您可以设置webview.HeightRequest设置后明确webview.Source

view = new WebView();
Content = new StackLayout
{
    Children = { view }
};
view.Navigated += View_Navigated;
//view.Source.BindingContextChanged += Source_BindingContextChanged;
//view.Source.PropertyChanged += Source_PropertyChanged;
HtmlWebViewSource mSource = new HtmlWebViewSource
{
    Html = @"<html><body>
           <h1>Test Code</h1>
           <p>The code is working.</p>
           </body></html>"
};
view.Source = mSource;
view.HeightRequest = 150;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM