首页 > 解决方案 > 使用 xamarin.forms 未完全加载 ListView 项目

问题描述

我的列表视图控制器不会加载所有元素,在这种情况下它只会加载 1 个元素,在其他情况下它会像 4 一样加载,但其他的则不会

当我测试问题发生的位置时,我知道问题出在 Frame 控制器上,这要归功于我的 List View 上的大多数项目都没有加载的框架

<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <Frame
          HeightRequest="100"
          Margin="10"
          HasShadow="True"
          CornerRadius="25"
          BackgroundColor="White">
        <Grid
            Padding="5">
          <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Label
              Grid.Column="0"
              Grid.Row="0"
              Text="{Binding Name}"
              FontAttributes="Bold"
              FontSize="Large"
              HorizontalOptions="Start"
              VerticalOptions="Start">
          </Label>
          <Image
              HeightRequest="50"
              Grid.Row="0"
              Grid.Column="1"
              Source="{Binding TaskIcon}"
              HorizontalOptions="End"
              VerticalOptions="Start">
          </Image>
          <Label
              Grid.Row="1"
              Grid.Column="0"
              Text="{Binding Description}"
              FontAttributes="Bold"
              FontSize="Medium"
              HorizontalOptions="Start"
              VerticalOptions="End">
          </Label>
          <Button
              Grid.Row="1"
              Grid.Column="1"
              Text="{Binding IsDone}"
              TextColor="White"
              FontAttributes="Bold"
              VerticalOptions="CenterAndExpand"
              HorizontalOptions="Center"
              FontSize="Small"
              CornerRadius="100"
              BackgroundColor="LawnGreen">
          </Button>
        </Grid>
      </Frame>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

我提到的结果是我的项目加载不完整,如下图所示:

问题截图

[已编辑] 我已将代码编辑如下,但错误仍然相同

<ListView
               SeparatorVisibility="None"
                    IsGroupingEnabled="True"
                    ItemsSource="{Binding TasksCollection}"
                    GroupDisplayBinding="{Binding Key}"
                    HasUnevenRows="True">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <TextCell
                                Text="{Binding Key}"
                                TextColor="White"/>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Frame
                                    HeightRequest="150"
                                    Margin="10"
                                    HasShadow="True"
                                    CornerRadius="25"
                                    BackgroundColor="White">
                                    <Grid
                                        Padding="5">
                                    <Grid.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding GridExpandCommand}"/>
                                    </Grid.GestureRecognizers>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="2*"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                        <Label
                                            Grid.Column="0"
                                            Grid.Row="0"
                                            Text="{Binding Name}"
                                            FontAttributes="Bold"
                                            FontSize="Small"
                                            HorizontalOptions="Start"
                                            VerticalOptions="Start">
                                        </Label>
                                        <Image
                                            HeightRequest="25"
                                            Grid.Row="0"
                                            Grid.Column="1"
                                            Source="{Binding TaskIcon}"
                                            HorizontalOptions="End"
                                            VerticalOptions="Start">
                                        </Image>
                                        <Label
                                            Grid.Row="1"
                                            Grid.Column="0"
                                            Text="{Binding Description}"
                                            FontAttributes="Bold"
                                            FontSize="Small"
                                            HorizontalOptions="Start"
                                            VerticalOptions="End">
                                        </Label>
                                        <Button
                                            Grid.Row="1"
                                            Grid.Column="1"
                                            Text="{Binding IsDone}"
                                            TextColor="White"
                                            FontAttributes="Bold"
                                            VerticalOptions="CenterAndExpand"
                                            HorizontalOptions="Center"
                                            FontSize="Small"
                                            CornerRadius="100"
                                            BackgroundColor="LawnGreen">
                                        </Button>
                                    </Grid>
                                </Frame>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

标签: xamarin.formsxamarin.androidxamarin.forms.listview

解决方案


仅当您的布局不适合时才会发生这种情况(除了您发送可能会看到的空数据的小问题),您试图将太大的项目放入太小的空间中。您应该设置图像的HeightRequestWidthRequest,并且可能还需要设置LineBreakMode标签。


推荐阅读