c# - 对于 IOS 和 Android,如何在标签或图像下方添加 Webview 并将所有元素滚动在一起?

标签 c# xaml xamarin xamarin.forms webview

我正在为 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/

相关文章:

silverlight - 将背景颜色应用于 XAML Silverlight 中的整个网格行

xamarin - 在 Xamarin Android 中存档时出现错误

exception - Xamarin System.Net.WebException 缺少 WebExceptionStatus.NameResolutionFailure

Android 以编程方式安装 .apk 文件

c# - EF6 生成查询垃圾 : Eliminate superfluous null checks

c# - 如何存储/检索 RSA 公钥/私钥

c# - if 或 else 之后的响应重定向

xaml - 实现多选项目列表

c# - Json.NET 无法序列化集合字典

c# - 多个源绑定(bind)到相同的目标依赖属性