我CollectionView.ItemsLayout
在绑定项目源后以编程方式更新它,它在 Android 上运行良好,但在 iOS 上运行良好。iOS 应用程序不会抛出任何异常/错误,但应用程序挂起,当我查看输出窗口时,我收到以下消息:
在计算布局(即重入调用)的过程中检测到更新布局信息的尝试。这将导致意外行为或崩溃。如果在调用委托时触发了布局传递,则可能会发生这种情况。UICollectionViewFlowLayout 实例是 (<Xamarin_Forms_Platform_iOS_GridViewLayout: 0x7fb2940ec010>)
在 prepareLayout 调用(即重入调用)正在进行时准备布局的尝试已被忽略。请提交一个错误。UICollectionView 实例是 (<UICollectionView: 0x7f9e68a73000; frame = (0 0; 355 360); clipsToBounds = YES; autoresize = W+H;gestureRecognizers = <NSArray: 0x600003039c20>; layer = <CALayer: 0x600007e6e180>; contentOffset: {0, 0};contentSize:{1、0};adjustedContentInset:{0、0、0、0};布局:<Xamarin_Forms_Platform_iOS_GridViewLayout:0x7f9e647972e0>;数据源:<Xamarin_Forms_Platform_iOS_GroupableItemsViewController_1:0x7f9e64797940>>)
我的 XAML:
<ScrollView>
<StackLayout Margin="0,0,5,0">
<Label x:Name="NoItemLabel" IsVisible="False"
Text="{languages:Translate EmptyListMessage}"
Style="{DynamicResource NoItemListLabel}" />
<!-- Product List -->
<CollectionView
VerticalScrollBarVisibility="Never" VerticalOptions="StartAndExpand"
HorizontalOptions="StartAndExpand"
x:Name="ItemsList" IsVisible="False" SelectionMode="Single"
SelectionChanged="ItemsList_SelectionChanged">
<CollectionView.ItemsLayout>
<GridItemsLayout x:Name="ItemsListItemsLayout" Span="{x:OnIdiom Tablet=3, Phone=2}" Orientation="Vertical" HorizontalItemSpacing="10" VerticalItemSpacing="10" />
</CollectionView.ItemsLayout>
<!--PAGINATION-->
<CollectionView.Header>
<Label
x:Name="ShowingTitle" Margin="5,10" TextColor="{DynamicResource LightGrayColor}"
FontSize="{StaticResource Medium}" HorizontalOptions="EndAndExpand"
VerticalOptions="CenterAndExpand" HorizontalTextAlignment="End" />
</CollectionView.Header>
<CollectionView.Footer>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Margin="0,10">
<Frame
x:Name="PreviousPageFrame" IsVisible="False"
Padding="5,5,10,5" Margin="0"
HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"
Style="{DynamicResource ButtonFrameStyle}">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="PreviousPage_Tapped" />
</Frame.GestureRecognizers>
<StackLayout Orientation="Horizontal">
<Image
Aspect="AspectFit" HorizontalOptions="EndAndExpand" Margin="0">
<Image.Source>
<FontImageSource
FontFamily="{DynamicResource MaterialFontFamily}"
Glyph="{x:Static font:FontAwesomeIcons.ThinLeft}" />
</Image.Source>
</Image>
<Label
FontSize="{StaticResource Small}"
Style="{DynamicResource ButtonLabelStyle}"
Text="{languages:Translate Previous}" Margin="-10,0,0,0"
TextColor="{DynamicResource SameThemeColor}" />
</StackLayout>
</Frame>
<Label
x:Name="PageNumber" HorizontalTextAlignment="Center"
FontSize="{StaticResource Medium}" TextColor="{DynamicResource DarkGrayColor}"
VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Frame
x:Name="NextPageFrame" IsVisible="False"
Padding="10,5,5,5" Margin="0"
HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"
Style="{DynamicResource ButtonFrameStyle}">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="NextPage_Tapped" />
</Frame.GestureRecognizers>
<StackLayout Orientation="Horizontal">
<Label
FontSize="{StaticResource Small}"
Style="{DynamicResource ButtonLabelStyle}"
Text="{languages:Translate Next}" Margin="0,0,-10,0" />
<Image
Aspect="AspectFit" HorizontalOptions="EndAndExpand" Margin="0">
<Image.Source>
<FontImageSource
FontFamily="{DynamicResource MaterialFontFamily}"
Glyph="{x:Static font:FontAwesomeIcons.ThinRight}"
Color="{DynamicResource SameThemeColor}" />
</Image.Source>
</Image>
</StackLayout>
</Frame>
</StackLayout>
</CollectionView.Footer>
<!--PAGINATION-->
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame
Margin="5" x:Name="productViewCake"
Padding="5" BackgroundColor="{DynamicResource SameThemeColor}"
CornerRadius="10" HasShadow="False" HorizontalOptions="StartAndExpand"
VerticalOptions="StartAndExpand">
<StackLayout>
<Image
Aspect="AspectFit"
HeightRequest="120"
Source="{Binding ItemID, Converter={StaticResource ImagePathConverter}, ConverterParameter='Item'}" />
<Label
FontFamily="{DynamicResource OpenSansSemiBold}" FontSize="{StaticResource Small}"
Text="{Binding ItemName}" LineBreakMode="TailTruncation"
Margin="0" Padding="0"
TextColor="{DynamicResource DarkGrayColor}"
VerticalOptions="CenterAndExpand" />
<Label
FontFamily="{DynamicResource OpenSansLight}"
FontSize="{StaticResource Small}"
HorizontalOptions="Start" Margin="0" Padding="0"
Text="{Binding ItemCategory.ItemCategoryName}" LineBreakMode="TailTruncation"
TextColor="{DynamicResource LightGrayColor}"
VerticalOptions="CenterAndExpand" />
<Label
FontFamily="{DynamicResource OpenSansBold}"
FontSize="{StaticResource Normal}"
HorizontalOptions="Start"
Text="{Binding CurrentPrice}"
TextColor="{DynamicResource AppThemeColor}"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ScrollView>
我的 XAML.cs:
internal async Task BindItems()
{
var jsonResponse = await WebApiService.ProcessRequestAsync(JsonMethods.GetItems);
var data = (JObject)JsonConvert.DeserializeObject(jsonResponse.ResponseJson);
int totalItems = data["TotalCount"].Value<int>();
var list = data["ItemMasters"].Value<JToken>().ToObject<List<ItemMaster>>();
ItemsList.RemoveBinding(ItemsView.ItemsSourceProperty);
//ItemsList.ItemsSource = null;
if (list.Count > 0)
{
ItemsList.SetBinding(ItemsView.ItemsSourceProperty, new Binding("list"));
//ItemsList.ItemsSource = list;
ItemsList.IsVisible = true;
NoItemLabel.IsVisible = false;
int widthPerElement = 170;
int span = (Application.Current.MainPage.Width / widthPerElement).ToProperInt();
ItemsListItemsLayout.Span = span;
totalPages = (totalItems / takeCount) + 1;
currentPage = (currentStartingCount / takeCount) + 1;
int showingTo = currentStartingCount + takeCount;
if (showingTo > totalItems) showingTo = totalItems;
ShowingTitle.Text = string.Format(TranslateExtension.Translate("ShowingTitle"), currentStartingCount + 1, showingTo, totalItems, TranslateExtension.Translate("Products").ToLowerInvariant());
PageNumber.Text = string.Format(TranslateExtension.Translate("PageNumber"), currentPage, totalPages);
//previous frame
if (currentPage <= 1 || totalPages <= 1)
{
PreviousPageFrame.IsVisible = false;
}
else
{
PreviousPageFrame.IsVisible = true;
}
//next frame
if (currentPage >= totalPages || totalPages <= 1)
{
NextPageFrame.IsVisible = false;
}
else
{
NextPageFrame.IsVisible = true;
}
}
else
{
ItemsList.IsVisible = false;
NoItemLabel.IsVisible = true;
}
}