C# · 12月 24, 2021

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

我编写了这段代码,试图在网页中显示几行 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; }

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

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

解决方法 确保为WebView指定layout-options. <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>