Xaml 仅在ScrollView中剪裁网格内容

Xaml 仅在ScrollView中剪裁网格内容,xaml,xamarin,layout,xamarin.forms,Xaml,Xamarin,Layout,Xamarin.forms,我有以下XAML和代码。由于某些原因,ScrollView中包含的网格被剪裁(第三行不显示)。我本以为所有东西都可以放在一个屏幕上,因为ListView可以垂直滚动,并且可以压缩得更多 如果我删除了ScrollView,并且网格与其他元素内联,所有内容都会按预期显示 为什么要剪裁第三行,如何使其始终显示 我使用的是Xamarin.Forms 2.5.0,但也可以在最新的3.1.0中复制,这在UWP、Android和iOS中都可以看到 XAML <?xml version="1.0" enc

我有以下XAML和代码。由于某些原因,ScrollView中包含的网格被剪裁(第三行不显示)。我本以为所有东西都可以放在一个屏幕上,因为ListView可以垂直滚动,并且可以压缩得更多

如果我删除了ScrollView,并且网格与其他元素内联,所有内容都会按预期显示

为什么要剪裁第三行,如何使其始终显示

我使用的是Xamarin.Forms 2.5.0,但也可以在最新的3.1.0中复制,这在UWP、Android和iOS中都可以看到

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"
    xmlns:local="clr-namespace:XamarinScrollView"
    x:Name="TheMainPage"            
    x:Class="XamarinScrollView.MainPage">

    <StackLayout BindingContext="{x:Reference TheMainPage}">
        <ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />
        <Label Text="{Binding StringValue}" LineBreakMode="WordWrap" />
        <Label Text="{Binding StringValue}" />
        <Label Text="{Binding StringValue}" LineBreakMode="WordWrap" />
        <Label Text="{Binding StringValue, StringFormat='String Value: {0}'}" IsVisible="{Binding HasStringValue}" />
        <Label Text="{Binding StringValue, StringFormat='String Value: {0}'}" />
        <Label Text="{Binding StringValue, StringFormat='String Value: {0}'}" />
        <BoxView HeightRequest="2">
            <BoxView.Color>
                <OnPlatform x:TypeArguments="Color">
                    <On Platform="Android">White</On>
                    <On Platform="iOS">Black</On>
                    <On Platform="UWP">Black</On>
                </OnPlatform>
            </BoxView.Color>
        </BoxView>
        <ScrollView Orientation="Horizontal">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" Text="{Binding DateValue, StringFormat='Date Value: {0:g}'}" />
                <Label Grid.Row="1" Grid.Column="0" Text="{Binding DateValue, StringFormat='Date Value: {0:g}'}" />
                <Label Grid.Row="2" Grid.Column="0" Text="{Binding StringValue, StringFormat='String Value: {0}'}" />

                <Label Grid.Row="0" Grid.Column="1" Text="{Binding DateValue, StringFormat='Date Value: {0:g}'}" />
                <Label Grid.Row="1" Grid.Column="1" Text="{Binding DateValue, StringFormat='Date Value: {0:g}'}" />
                <Label Grid.Row="2" Grid.Column="1" Text="{Binding DateValue, StringFormat='Date Value: {0:g}'}" />
            </Grid>
        </ScrollView>
        <Frame Margin="2">
            <Frame.BorderColor>
                <OnPlatform x:TypeArguments="Color">
                    <On Platform="Android">White</On>
                    <On Platform="iOS">Black</On>
                    <On Platform="UWP">Black</On>
                </OnPlatform>
            </Frame.BorderColor>

            <StackLayout Padding="0,0,0,20">
                <Label Text="String Value" />
                <Label Text="{Binding StringValue}" LineBreakMode="WordWrap" />
            </StackLayout>
        </Frame>
        <Frame Margin="2">
            <Frame.BorderColor>
                <OnPlatform x:TypeArguments="Color">
                    <On Platform="Android">White</On>
                    <On Platform="iOS">Black</On>
                    <On Platform="UWP">Black</On>
                </OnPlatform>
            </Frame.BorderColor>

            <ListView ItemsSource="{Binding ListItems}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="0,0,0,10">
                                <Label Text="{Binding StringValue1}" />
                                <Label Text="{Binding StringValue2}" />
                                <Label Text="{Binding StringValue3}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Frame>
    </StackLayout>
</ContentPage>

白色
黑色
黑色
白色
黑色
黑色
白色
黑色
黑色
代码隐藏

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace XamarinScrollView {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
        }

        public string StringValue => "This is a string";

        public DateTime DateValue => DateTime.Now;

        public bool HasStringValue => true;

        private readonly List<ListItem> listItems = new List<ListItem> { 
            new ListItem(),
        };
        public IEnumerable<ListItem> ListItems => listItems;
    }

    public sealed class ListItem { 
        public string StringValue1 => "1. This is a string";
        public string StringValue2 => "2. This is a string";
        public string StringValue3 => "3. This is a string";
    }
}
使用系统;
使用System.Collections.Generic;
使用Xamarin.Forms;
名称空间视图{
公共部分类主页:ContentPage{
公共主页(){
初始化组件();
}
公共字符串StringValue=>“这是一个字符串”;
public DateTime DateValue=>DateTime.Now;
公共bool HasStringValue=>true;
私有只读列表listItems=新列表{
新建ListItem(),
};
public IEnumerable ListItems=>ListItems;
}
公共密封类列表项{
公共字符串StringValue1=>“1.这是一个字符串”;
公共字符串StringValue2=>“2.这是一个字符串”;
公共字符串StringValue3=>“3.这是一个字符串”;
}
}

根据ListView中的项目数量,ScrollView将缩小,以便ListView有足够的空间显示其项目


即使您的网格有
Auto
行,并且将创建适合所显示内容的行,
ScrollView
将收缩到某一点。
网格
仍有其内容所需的空间量,但ScrollView仅显示空间所能显示的内容ScrollView
堆栈布局
中给出。由于您只允许滚动视图的
水平方向,因此无法滚动查看网格的最后一行。

您有一个滚动视图,但不希望剪切元素。您的解决方案是向ScrollView提供一个HeightRequest。

在ScrollView和包含的GridView上设置MinimumHeightRequest似乎可以解决这个问题。

即使列表视图中只有一个项目,并且屏幕下部有足够的空间,这种情况似乎也会发生。scrollview的目的是以某种方式将正确的列移出屏幕,以便用户可以来回滑动以查看第一列和第二列的内容,但实现这一点将是另一个问题。堆栈布局不会扩展每个子级以填充最大空间。尝试将ScollView
VerticalOptions
设置为
FillAndExpand
。这样行吗?
FillAndExpand
行不通。最外层的
StackLayout
已经占据了整个页面;即使我最大化,网格的最后一行仍然被剪裁。我确实认为这与
列表视图
有关;它似乎有一些大的最小尺寸,即使只有一个项目。奇怪,因为它可以滚动,所以不需要占用太多的空间。因为这个答案让我找到了正确的答案,所以我给了赏金。你最终使用了@Andy做了什么?我发布了我自己的答案,但在网格和滚动视图上都设置了mimumheightrequest。