[英]Adding html to a webview is appearing as blank iOS xamarin forms
我在Xamarin.forms項目中有一個Web視圖,它由從數據庫加載的一些HTML填充。
以下是創建Web視圖的XAML代碼。 可能值得注意的是,這是在GridView內部。
<WebView Grid.Row="2" x:Name="policyView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
這是用於加載和設置Web視圖源的CS代碼,以便它可以顯示HTML。
public async void LoadPolicy(policy_procedure policy)
{
busyIndicator.IsVisible = true;
busyMessage.Text = "Loading Policy";
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = await ViewModelLocator.ConvertPolicy(policy);// returns the html as a string
policyView.Source = htmlSource;
busyIndicator.IsVisible = false;
}
該代碼在Android上運行良好,並且可以毫無問題地顯示HTML內容。 但是,當在iOS上運行時,我只是出現一個空白屏幕,我不確定為什么會這樣。 我試過將水平選項設置為fillAndExpand
同時設置垂直選項。 我的問題是如何使HTML代碼出現在iOS上?
謝謝。
發生這種情況是因為您沒有提供AppTransportSecurity(ATS)。 轉到您的info.plist並添加權限。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
供參考: https : //developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
我在iOS中測試過以下代碼。 您可以根據需要進行調整。
<ContentPage.Content>
<StackLayout BackgroundColor="Teal" TranslationY="64">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" BackgroundColor="Silver" Text="tets test" HeightRequest="30"
HorizontalOptions="FillAndExpand"/>
<WebView Grid.Row="1" Grid.Column="1" BackgroundColor="Red" x:Name="webView"
VerticalOptions="FillAndExpand" HeightRequest="300"
HorizontalOptions="FillAndExpand" />
</Grid>
</StackLayout>
</ContentPage.Content>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.