ListView 未在 Xamarin 表单中完全显示

问题描述

我有一个 ListView,里面有大约 10 个项目。 ListView 需要完全显示在屏幕上。但它没有显示。尝试使用 ScrollView,但未显示所有项目,并且无法滚动到视图中的最后一个项目。

附上我的源代码以供参考。 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"
             x:Class="KFM.View.TripPage" Title="Trips" > 
    <ContentPage.Content>
        <StackLayout Orientation="Vertical" Spacing="30" VerticalOptions="FillAndExpand">
            <ScrollView VerticalOptions="FillAndExpand">
            <ListView x:Name="MenuListView" ItemsSource="{Binding Trips}"  HorizontalOptions="FillAndExpand" HasUnevenRows="True" SeparatorVisibility="None" IsEnabled="False" VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Padding="5">
                                <Frame HasShadow="true" HeightRequest="100" BorderColor="Black">
                                    <Grid>
                                        <Grid.ColumnDeFinitions>
                                            <ColumnDeFinition Width="10"/>
                                            <ColumnDeFinition Width="*"/>
                                        </Grid.ColumnDeFinitions>
                                        <StackLayout Grid.Column="0" Orientation="Vertical" VerticalOptions="FillAndExpand">
                                            <BoxView Color="Blue" VerticalOptions="FillAndExpand"/>
                                        </StackLayout>
                                        <StackLayout Grid.Column="1" Orientation="Vertical" VerticalOptions="FillAndExpand">
                                            <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                                <Label Text="{Binding DateFrom,StringFormat='{}{0:MMM - dd HH:mm}'}" TextColor="Black" FontSize="20"/>
                                                <Label Text=" to " TextColor="Black" FontSize="20"/>
                                                <Label Text="{Binding Dateto,StringFormat='{}{0:MMM - dd HH:mm}'}" TextColor="Black" FontSize="20"/>
                                            </StackLayout>
                                            <Label Text="Details" TextColor="Black" FontSize="16" Textdecorations="Underline"/>
                                            <Label Text="{Binding Place}" TextColor="Black" FontSize="20"/>
                                        </StackLayout>
                                    </Grid>
                                </Frame>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            </ScrollView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

后端 C# 代码

Trips = new List<Trip>()
            {
                new Trip() { DateFrom = DateTime.Now,Dateto = DateTime.Now.AddDays(2),Place="Chennai to trichy" },new Trip() { DateFrom = DateTime.Now.AddDays(7),Dateto = DateTime.Now.AddDays(9),Place="Chennai to bangalore" },new Trip() { DateFrom = DateTime.Now.AddDays(12),Dateto = DateTime.Now.AddDays(14),Place="Local" },new Trip() { DateFrom = DateTime.Now.AddDays(16),Dateto = DateTime.Now.AddDays(18),Place="Chennai to Salem" },new Trip() { DateFrom = DateTime.Now.AddDays(18),Dateto = DateTime.Now.AddDays(22),Place="Chennai to Tirunelveli" },new Trip() { DateFrom = DateTime.Now.AddDays(24),Dateto = DateTime.Now.AddDays(30),Place="Chennai to Mysore" },new Trip() { DateFrom = DateTime.Now.AddDays(32),Dateto = DateTime.Now.AddDays(34),new Trip() { DateFrom = DateTime.Now.AddDays(36),Dateto = DateTime.Now.AddDays(40),};

解决方法

那是因为您将 ListView 放在 ScrollView 中。在这种情况下,ScrollView 无法在运行时获得 ListView 的当前高度。既然你用过 ListView ,为什么还要放在 ScrollView 里?

我使用了以下代码,效果很好

<StackLayout Orientation="Vertical"  VerticalOptions="FillAndExpand">
       
    <ListView x:Name="MenuListView"   HorizontalOptions="FillAndExpand" HasUnevenRows="True" SeparatorVisibility="None"  VerticalOptions="FillAndExpand">
               //...
    </ListView>
      
</StackLayout>