如何在C#中使用HTML源代码在Xamarin.Forms页面中显示HTML?

Ala*_*an2 6 xamarin xamarin.forms

我编写了这段代码,试图在网页中显示几行HTML:

我有这个XAML:

 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:Japanese;assembly=Test" 
    x:Class="Test.HelpCards" 
    x:Name="HelpCards" 
    Title="Help ? Cards Tab">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout Spacing="10" Margin="20">
               <WebView x:Name="Browser" />
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

    public HelpCards()
    {
        InitializeComponent();
        var htmlSource = new HtmlWebViewSource();
        htmlSource.Html = @"<html><body>
        <h1>ABC</h1>
        <p>DEF</p>
        </body></html>";
        Browser.Source = htmlSource;
    }
Run Code Online (Sandbox Code Playgroud)

但是,在运行代码时,我只看到一个空白页面.

有没有人对什么可能是错的有任何想法?

Sha*_*raj 6

确保为其指定layout-options WebView.

<ContentPage.Content>
    <ScrollView> <!-- ScrollView not needed as WebView has inbuilt scrolling behavior -->
        <StackLayout Spacing="10" Margin="20">
           <WebView x:Name="Browser" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
        </StackLayout>
    </ScrollView>
</ContentPage.Content>
Run Code Online (Sandbox Code Playgroud)

  • 对不起,我在水平选项中设置了`StartAndExpand` - 它应该是`FillAndExpand` (2认同)

小智 5

Xamarin 在某个时候更新了该label元素。label该更新包括一种通过添加到元素来在元素中显示 HTML 的方法TextType="Html"

例子:

<Label TextType="Html">
    <![CDATA[
    This is <strong style="color:red">HTML</strong> text.
    ]]>
</Label>
Run Code Online (Sandbox Code Playgroud)

(来源: https: //learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#display-html