Collection视图在IOS上不显示项目,但在Android上工作



我试图在点击按钮时在collectionview上显示我的项目。它可以在Android模拟器上工作,当从VS.部署Android时,但它不能在IOS模拟器上工作

下面是我的xaml代码:
<StackLayout x:Name="WordSListStack"  IsVisible="False"  Padding="0,10,0,0">

<CollectionView x:Name="WordSList"  
ClassId="1"  SelectionMode="Single"   
SelectionChanged="OnSelectedWordAddDatabase"  >

<CollectionView.ItemTemplate>
<DataTemplate >
<StackLayout BackgroundColor="White" Padding="10"  Margin="5"  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical">
<Label TextColor="#7D7D7D" Text="{Binding .}"  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"  FontSize="16" />
</StackLayout>

</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

</StackLayout>

下面是我如何在collectionview上加载项目:

private async void OnSelectedLocalPlace(object sender, SelectionChangedEventArgs e)
{

Device.BeginInvokeOnMainThread(() =>
{
WordSListStack.IsVisible=true;
WordSList.ItemsSource = LocalWordsList;
});

}

错误如下:

The relevant UICollectionViewFlowLayout instance is <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>, and it is attached to <UICollectionView: 0x7faac13c0e00; frame = (0 0; 796 823); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000031966d0>; layer = <CALayer: 0x60000354ac00>; contentOffset: {0, 0}; contentSize: {797, 368}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7faaf06e4c50>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
The behavior of the UICollectionViewFlowLayout is not defined because:
the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
Please check the values returned by the delegate.
The relevant UICollectionViewFlowLayout instance is <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>, and it is attached to <UICollectionView: 0x7faac13c0e00; frame = (0 0; 796 823); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000031966d0>; layer = <CALayer: 0x60000354ac00>; contentOffset: {0, 0}; contentSize: {797, 368}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>; dataSource: <Xamarin_Forms_Plat
form_iOS_GroupableItemsViewController_1: 0x7faaf06e4c50>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
The behavior of the UICollectionViewFlowLayout is not defined because:
the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
Please check the values returned by the delegate.
The relevant UICollectionViewFlowLayout instance is <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>, and it is attached to <UICollectionView: 0x7faac13c0e00; frame = (0 0; 796 823); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000031966d0>; layer = <CALayer: 0x60000354ac00>; contentOffset: {0, 0}; contentSize: {797, 368}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7faaf06e4c50>>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
The behavior of the UICollectionViewFlowLayout is not defined because:
the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
Please check the values returned by the delegate.
The relevant UICollectionViewFlowLayout instance is <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>, and it is attached to <UICollectionView: 0x7faac13c0e00; frame = (0 0; 796 823); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x6000031966d0>; layer = <CALayer: 0x60000354ac00>; contentOffset: {0, 0}; contentSize: {797, 368}; adjustedContentInset: {0, 0, 0, 0}; layout: <Xamarin_Forms_Platform_iOS_ListViewLayout: 0x7faaf06e4a20>; dataSource: <Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1: 0x7faaf06e4c50>>.
2021-07-30 13:47:17.671882+0200 LeafWords.iOS[53379:807398] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

我有这个问题很长时间了,以至于我去了所有的listview。

事实证明,为了让CollectionView在iOS中工作,你必须设置一个SizingStrategy。我不知道是不是没有默认值,但这就是阻止我的原因。

ItemSizingStrategy ="MeasureFirstItem"或ItemSizingStrategy ="MeasureAllItems">

这似乎是一个bug: https://github.com/xamarin/Xamarin.Forms/issues/13323

根据该页面上的信息,您可以使用Xamarin。Forms 5.0预发布6来避免这个问题。很可能以前的一些常规版本也可以正常工作。你也可以尝试将ItemSizingStrategy设置为MeasureFirstItem,如果它适合你的用例。

同样来自Ivan回答中提到的链接,建议在显示项目之前使用await Task.Delay(50);

试题:

private async void OnSelectedLocalPlace(object sender, SelectionChangedEventArgs e)
{
await Task.Delay(50);
Device.BeginInvokeOnMainThread(async () =>
{
WordSList.ItemsSource = LocalWordsList;
await Task.Delay(50);
WordSListStack.IsVisible=true;
await Task.Delay(50);
});
}

重要:我在Device.BeginInvoke..行上添加了async。这是需要的,以便下面的await将按预期工作。

注意:我已经在多个地方展示了延迟。试着把它们全部去掉,如果可以的话,试着把其中一些去掉。

相关内容

最新更新