Xamarin.forms Xamarin表单:WebView数据绑定
我正在尝试构建一个页面,该页面使用数据绑定来填充在StackLayout上托管的WebView。以下是该内容页的XAML:Xamarin.forms Xamarin表单:WebView数据绑定,xamarin.forms,xamarin.android,android-webview,Xamarin.forms,Xamarin.android,Android Webview,我正在尝试构建一个页面,该页面使用数据绑定来填充在StackLayout上托管的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"
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GES.Views.DetailsPage"
xmlns:di="clr-namespace:GES.DI;assembly=GES"
xmlns:c="clr-namespace:GES.Views.Converters;assembly=GES"
di:ServiceLocator.AutoWireViewModel="true"
Padding="15, 10, 15, 10"
Title="Ensino Superior">
<ContentPage.Resources>
<ResourceDictionary>
<c:HtmlSourceConverter x:Key="htmlConverter"/>
<Style x:Key="title" TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="TextColor" Value="{StaticResource colorPrimary}" />
<Setter Property="FontSize" Value="16" />
</Style>
<Style x:Key="dateText" TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource lightGray}" />
<Setter Property="FontSize" Value="10"></Setter>
<Setter Property="FontAttributes" Value="Bold"></Setter>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<ScrollView>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="{Binding Path=News.Title}"
Margin="0, 10, 0, 10"
Style="{StaticResource title}"></Label>
<Label Text="{Binding Path=News.PublicationDate, StringFormat='{0:dd/MM/yyyy}'}"
HorizontalTextAlignment="End"
Margin="0, 0, 10, 10"
Style="{StaticResource dateText}"></Label>
<WebView Margin="0, 10, 0, 10"
Source="{Binding Path=News.Contents, Converter={StaticResource htmlConverter}}">
</WebView>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
现在,问题来了。加载页面时,WebView组件只会呈现一个小的白色矩形…旋转设备,例如:从portrai到横向,反之亦然,会显示WebView的内容。例如,假设我在纵向模式下使用设备。加载页面时显示以下内容:
但是,如果我旋转设备,它将显示以下内容:
看起来WebView在第一次加载时没有正确呈现…顺便说一句,如果我在横向加载页面,那么我将不得不转换为纵向模式来查看任何内容
我试着在网上搜索,我确实找到了一个,但它似乎已经被修复了。我正在使用最新稳定版本的Xamarin Forms assembly
有什么线索吗?对于任何感兴趣的人,我已经按照此创建了自定义webview渲染器…您是否尝试过显式设置webview的高度?而不是要求的高度。。。虽然我尝试过使用其中一个填充选项……但是webview存在一个问题,即高度没有动态更新,并且高度仍然保持为0,与开始时一样。让我知道它是否有效。
public class HtmlSourceConverter: IValueConverter{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture) {
var html = new HtmlWebViewSource();
if (value is string h &&
!string.IsNullOrEmpty(h)) {
html.Html = HttpUtility.HtmlDecode(h);
}
return html;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture) =>
throw new NotImplementedException();
}