我编写了以下代码,以尝试在网页中显示几行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>