【问题标题】:Returning WebView from ContentView in Xamarin从 Xamarin 中的 ContentView 返回 WebView
【发布时间】:2019-02-11 00:13:22
【问题描述】:

我正在尝试在我的 Xamarin 应用程序的另一个页面中使用自定义 ContentView。此自定义 ContentView 用于显示 HTML 内容(特别是 gif)。尝试从另一个页面加载 ContentView 时,会抛出错误:“Value cannot be null.Parameter name thisActivity”

我的代码基于here 中的示例。

我是否错过了通过自定义 ContentView 呈现 WebView 的关键步骤?

//这是我的自定义内容视图

public class GifView : ContentView
{
    public GifView()
    {
        this.BuildPage();
    }

    public WebView BuildWebView()
    {
        var htmlSource = new HtmlWebViewSource
        {
            Html = @"<html><body><h1>This is a test</h1></body></html>"
        };

        WebView webView = new WebView
        {
            WidthRequest = 1000,
            HeightRequest = 1000,
            Source = htmlSource
        };

        return webView;
    }

    public void BuildPage()
    {
        var webView = this.BuildWebView();

        this.Content = new StackLayout
        {
            Children =
            {
                webView
            }
        };
    }
}

// 这是我尝试使用自定义视图的 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"
                 xmlns:local="clr-namespace:myproject"
                 x:Class="myproject.MainPage"
                 xmlns:views="clr-namespace:myproject.Views">
            <views:GifView />

    </ContentPage>

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    好的,这是一个非常愚蠢的错误。该解决方案部署良好,可在 Android 手机上运行。我猜 Visual Studio 的设计功能有问题,并显示不正确的错误。

    【讨论】:

      猜你喜欢
      • 2016-03-22
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      相关资源
      最近更新 更多