gpt4 book ai didi

c# - Xamarin WebView 通常不显示任何内容

转载 作者:行者123 更新时间:2023-11-29 19:01:24 24 4
gpt4 key购买 nike

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 代码。

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

断点并没有告诉我太多。监视列表毫无用处,无法告诉我任何信息。通过单步执行,我可以看到在我的各种测试用例中都达到了正确的代码路径,但仅此而已。

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

相应的 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>

编辑:

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. 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 永远不会根据其网页内容重新测量。

所以有两种解决方法:

  1. 在 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. 在 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 后:

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;

关于c# - Xamarin WebView 通常不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com