CollectionView-以编程方式更新ItemsLayout时出现的问题

问题描述

在绑定项目源之后,我正在以编程方式更新CollectionView.ItemsLayout,它在Android上工作正常,但在iOS上却无法正常工作。 iOS应用程序未引发任何异常/错误,但该应用程序挂起,当我查看输出窗口时,收到以下消息:

在计算布局(即重入调用)的过程中,已检测到尝试更新布局信息。这将导致意外行为或崩溃。如果在调出代表时触发了布局传递,则可能会发生这种情况。 UICollectionViewFlowLayout实例为(

在进行prepareLayout调用(即重入调用)时准备布局的尝试已被忽略。请提交错误。 UICollectionView实例为(; layer = ; contentOffset:{0, 0}; contentSize:{1、0}; adjustedContentInset:{0、0、0、0};布局:;数据源:>)

我的XAML:

   <ScrollView>
        <StackLayout Margin="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,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"
                                    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" 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,-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;
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)