我正在为 IOS 和 Android 开发 Xamarin.Forms 应用程序。我在XAML文件中的代码是这样的:
<StackLayout BackgroundColor="White" Padding="1" VerticalOptions="FillAndExpand">
<Image HeightRequest="150" Aspect="AspectFill" Source="{Binding Post.ImageUrl}" />
<Label Text="{Binding Post.Title}" VerticalOptions="FillAndExpand" FontFamily="{StaticResource BoldFont}" FontSize="20" />
<Label Text="{Binding Post.CreatedOn}" VerticalOptions="FillAndExpand" FontFamily="{StaticResource NormalFont}" FontSize="12" />
<WebView x:Name="WebViewer" HeightRequest="500" VerticalOptions="FillAndExpand" BackgroundColor="White">
<WebView.Source>
<HtmlWebViewSource Html="{Binding Post.Data.Content}" />
</WebView.Source>
</WebView>
<Label Text="Comments" FontSize="12" />
</StackLayout>
当我模拟应用程序时,只有 Webview 滚动和所有其他元素保持固定。 Stackoverflow 上还有另一个类似的问题,但该解决方案不起作用,并且它不能回答我针对 IOS 的问题,它仅针对 Android。
This image shows the webview scrolling, and it's siblings staying in place
我希望它们都一起滚动,这样它就像一页一样。这应该是一个跨平台的解决方案,所以我只需要为 IOS 和 Android 编写一次代码。我怎样才能实现这个目标?
最佳答案
你可以尝试这样的事情
设计方面
<?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="XamarinTest.View.WebViewDemo">
<ContentPage.Content>
<ScrollView>
<StackLayout BackgroundColor="White" Padding="1" VerticalOptions="FillAndExpand">
<Image HeightRequest="150" Aspect="AspectFill" Source="home" />
<Label Text="Header" FontSize="20" />
<Label Text="Subheader" FontSize="12" />
<WebView Source="http://www.google.com" x:Name="WebViewer" BackgroundColor="White">
</WebView>
<Label Text="Comments" FontSize="12" />
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
编码端
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace XamarinTest.View
{
public partial class WebViewDemo : ContentPage
{
public WebViewDemo()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
WebViewer.HeightRequest = height;
WebViewer.WidthRequest = width;
}
}
}
关于c# - 对于 IOS 和 Android,如何在标签或图像下方添加 Webview 并将所有元素滚动在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56053700/