网格内容仅在ScrollView中被裁剪

时间:2018-06-27 13:54:14

标签: 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" 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";
    }
}

3 个答案:

答案 0 :(得分:0)

取决于ListView中项目的数量,ScrollView将缩小,因此ListView有足够的空间来显示其项目。

即使您的网格有Auto行,并且将创建适合所显示内容的行,但ScrollView仍会缩小到某个点。 Grid仍具有其内容所需的空间量,但是ScrollView仅显示在StackLayout中给出的 ScrollView 空间。由于您只允许Horizontal的ScrollView方向,所以这就是为什么您无法滚动以查看网格的最后一行。

答案 1 :(得分:0)

您有一个ScrollView,但是,您不需要剪切元素。您的解决方案是向ScrollView提供一个HeightRequest。

答案 2 :(得分:0)

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

相关问题