【问题标题】:Grid content is clipped only in ScrollView网格内容仅在 ScrollView 中被裁剪
【发布时间】:2018-12-06 11:01:42
【问题描述】:

我有以下 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";
    }
}

【问题讨论】:

    标签: xaml xamarin layout xamarin.forms


    【解决方案1】:

    您有一个 ScrollView,但是,您不希望元素被剪裁。您的解决方案是向 ScrollView 提供 HeightRequest。

    【讨论】:

    • 因为这个答案让我找到了正确的答案,所以我给予赏金。
    • 你最后用@Andy做了什么?
    • 我发布了自己的答案,但在网格和滚动视图上都设置了 MimimumHeightRequest。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      根据 ListView 中的项目数,ScrollView 会缩小,因此 ListView 有足够的空间来显示其项目。

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

      【讨论】:

      • 即使列表视图中只有一个项目,并且屏幕下部有足够的空间,这似乎也会发生。滚动视图的目的是以某种方式使右列离开屏幕,以便用户可以来回滑动以查看第一列和第二列的内容,但实现这将是另一个问题。
      • 堆栈布局不会扩展每个子元素以填充最大空间。尝试将 ScollView VerticalOptions 设置为 FillAndExpand。这行得通吗?
      • FillAndExpand 不起作用。最外面的StackLayout 已经占据了整个页面;即使我最大化,Grid 的最后一行仍然被剪裁。我确实认为这与ListView有关;即使只有一件物品,它的最小尺寸似乎也很大。很奇怪,因为它可以滚动,所以它不需要占用太多空间。
      猜你喜欢
      • 2023-03-11
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多