Xamarin WebView usually shows nothing

被刻印的时光 ゝ 提交于 2021-02-08 09:27:28

问题


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

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.

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>

EDIT:

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>

回答1:


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.

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.

So there are two workarounds:

  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>
    
  2. Give a HeightRequest in webview's 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.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;


来源:https://stackoverflow.com/questions/48981867/xamarin-webview-usually-shows-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!