使用xamarin.forms未完全加载ListView项

时间:2019-10-20 19:10:42

标签: xamarin.forms xamarin.android xamarin.forms.listview

我的List View控制器不会加载所有元素,在这种情况下只加载1个元素,在其他情况下,它加载像4个元素,而其他情况则不加载

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

<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>

我所提到的结果是我的商品未完全加载,如下图所示:

Screenshot of the problem

[已编辑] 我已经按照以下方式编辑了代码,但错误仍然相同

<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>

2 个答案:

答案 0 :(得分:0)

仅当您的布局无法容纳时(除了琐碎的问题,您正在发送可能会看到的空数据),您还试图将太大的项目容纳在太小的空间中,才会发生这种情况。您应该设置图像的HeightRequestWidthRequest,并且可能还需要为标签设置LineBreakMode

答案 1 :(得分:0)

我设置了ListView HasUnevenRows =“ True” ,它工作正常,没有任何问题。

<ListView HasUnevenRows="True" ItemsSource="{Binding models}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame
                            Margin="10"
                            BackgroundColor="White"
                            CornerRadius="25"
                            HasShadow="True"
                            HeightRequest="120">
                            <Grid Padding="5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="2*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Label
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    FontAttributes="Bold"
                                    FontSize="Large"
                                    Text="{Binding Name}" />
                                <Image
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Source="{Binding TaskIcon}" />
                                <Label
                                    Grid.Row="1"
                                    Grid.Column="0"
                                    FontAttributes="Bold"
                                    FontSize="Medium"
                                    Text="{Binding Description}" />
                                <Button
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    BackgroundColor="LawnGreen"
                                    CornerRadius="100"
                                    FontAttributes="Bold"
                                    FontSize="Small"
                                    Text="{Binding IsDone}"
                                    TextColor="White" />
                            </Grid>
                        </Frame>
                    </ViewCell>
                </DataTemplate>

            </ListView.ItemTemplate>
        </ListView>