Xamarin WebView 通常什么都不显示 [英] Xamarin WebView usually shows nothing

查看:34
本文介绍了Xamarin WebView 通常什么都不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ...

页面大部分时间都显示为空白.在 xamarin.com 示例中,经过长时间的延迟后,我运行该页面的次数约为 20%.谷歌和自定义测试根本不起作用.自定义代码是我想要在某些特定情况下使用的方法,这些情况更易于创建为 Web 代码.

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. The google and custom tests don't work at all. The custom one is what I want working for some particular cases that will be easier to create as web code.

在我的其他情况下,我创建和添加的其他视图组件工作得很好.(按钮、标签等...)

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.

测试是在我的 Android 手机上通过 Xamarin Live Player (Android 6.0 - API 23) 完成的.代码在 Visual Studio 2015 中.目标平台为 Android 和 iOS.

Tests are done on my Android phone in the Xamarin Live Player (Android 6.0 - API 23). The code is in Visual Studio 2015. Target platforms are both Android and iOS.

对应的 xaml 页面没有太多内容.

The corresponding xaml page doesn't have much to it.

<?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>

AndroidManifext.xml 具有 INTERNET 权限:

AndroidManifext.xml has the INTERNET permission:

<?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>

推荐答案

页面大部分时间都显示为空白.在 xamarin.com 示例中,经过长时间的延迟后,我运行该页面的次数约为 20%.谷歌和自定义测试根本不起作用.自定义代码是我想要在某些特定情况下使用的方法,这些情况更易于创建为 Web 代码.

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. The google and custom tests don't work at all. The custom one is what I want working for some particular cases that will be easier to create as web code.

我已经对其进行了测试并重现了该问题.只有当您在构造函数或 OnAppearing 中以编程方式创建 webview 时,才会发生这种情况.我设置了断点,发现 webview 的高度不会自动扩展(始终为 0).我的猜测是,如果你在页面的构造函数或 OnAppearing 中创建 webview,webview 永远不会根据它的 web 内容重新测量.

I've tested it and reproduced the problem. This only happens when you are creating webview programmatically in constructor or in OnAppearing. I set breakpoint and found that the webview's height doesn't get automatically expanded (always 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.

所以有两种解决方法:

  1. 在 Xaml 中使用 webview:

  1. Use webview in Xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     ...>
    <WebView Source="http://xamarin.com"></WebView>
</ContentPage>

  • 在 webview 的 OnNavigated 中给出一个 HeightRequest:

    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;
    }
    

  • 更新:如果您使用 HtmlWebViewSource 作为 webview 的源代码.View_Navigated 不会被触发.所以在这种情况下,你可以设置webview.HeightRequest 在设置 webview.Source 后显式设置:

    Update: If you are using HtmlWebViewSource for your webview's Source.View_Navigated won't get triggered. So in such circumsance, you can set webview.HeightRequest explicitly after setting 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;
    

    这篇关于Xamarin WebView 通常什么都不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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