XAML 我无法显示所有CollectionView项目,NET MAUI应用程序

hzbexzde  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(160)

我的.NET MAUI应用程序有问题。我试图显示从我的API检索的测验列表。问题是只显示前两个测验。我有6个项目,所以通常情况下,6个项目应显示2个项目每行。我试着删除其中一个2测验和第三个得到显示的地方,我删除了。
这是我的QuizHome设计:

<ScrollView>
  <VerticalStackLayout>
    <Grid RowDefinitions="Auto,Auto,Auto" ColumnDefinitions="Auto,*" ColumnSpacing="10">
      <Grid Grid.Row="0" Grid.ColumnSpan="2">
        <SearchBar Placeholder="Search" PlaceholderColor="#49B1F9" HorizontalTextAlignment="Center" CancelButtonColor="#49B1F9"/>
      </Grid>
      <Label Grid.Row="1" Text="Choose Topic" Style="{StaticResource BaseMediumDarkLabelTextStyle}" FontSize="20" Margin="5,15" />
      <CollectionView ItemsSource="{Binding QuizesVM}"  Grid.Row="2"  Margin="30,0,0,20">
        <CollectionView.ItemsLayout>
          <GridItemsLayout Orientation="Vertical" Span="2" />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
          <DataTemplate x:DataType="models:QuizVM">
            <Grid RowDefinitions="Auto,Auto" Margin="8,0,8,0" HeightRequest="200" WidthRequest="150">
              <Grid.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:QuizViewModel}},Path=GotoQuizDetailsCommand}" CommandParameter="{Binding .}"/>
              </Grid.GestureRecognizers>
              <Border StrokeShape="RoundRectangle 15 15 15 15 " StrokeThickness="2" HeightRequest="160" WidthRequest="150">
                <Label Grid.Row="0" Text="{Binding QuizTitle}" HorizontalOptions="Center" VerticalOptions="Center" Style="{StaticResource BaseMediumDarkLabelTextStyle}"></Label>
              </Border>
            </Grid>
          </DataTemplate>
        </CollectionView.ItemTemplate>
      </CollectionView>
    </Grid>
  </VerticalStackLayout>
</ScrollView>

字符串

t98cgbkg

t98cgbkg1#

从API接收到数据列表后,必须调用OnPropertyChanged(name)。就像那个简短的代码示例一样。

QuizesVM = new ObservableCollection<ModelClass>(){
   new ModelClass() {data},
   new ModelClass() {data},
   //...
} 
//Then call this "OnPropertyChaged(your binding Property name) to appear items in CollectionView on screen.
OnPropertyChanged(nameof(QuizesVM));

字符串

相关问题