Xamarin Forms: CollectionView is not working in ios

ε祈祈猫儿з 提交于 2019-12-13 03:32:37

问题


I am using CollectionView for the horizontal listview. It is working fine in android but in ios, the items are showing on one top of another. Attaching a screenshot below:

XAML

<CollectionView 
             HeightRequest="30"
             SelectionMode="Single"
             SelectionChanged="ItemTapped"
        ItemsSource="{Binding Items}"
             x:Name="collectionview"
             ItemsLayout="HorizontalList">
             <CollectionView.ItemTemplate>
                   <DataTemplate>
                          <StackLayout Margin="5">
                                  <Label
                                       TextColor="Black"
                                       FontSize="Large"
                                       HorizontalTextAlignment="Center"
                                       VerticalTextAlignment="Center"
                                       Text="{Binding title}"/>
                             </StackLayout>
                        </DataTemplate>
                 </CollectionView.ItemTemplate>
          </CollectionView>

Xaml.cs

public async void ItemTapped(object sender, SelectionChangedEventArgs e)
        {
            var selectedItem = (e.CurrentSelection.FirstOrDefault() as MyModel);
            if (selectedItem != null)
            {
                //Do action
            }
        }

Add below code in AppDelegate class on iOS and MainActivity class on Android, before calling Forms.Init:

Forms.SetFlags("CollectionView_Experimental");

Am I missing something in IOS?


回答1:


It seems an existing issue of Xamarin.Forms . As workaround you can update the version of Xamarin.Forms to 4.4 pre2 .

And you can check the Xamarin.forms release notes .




回答2:


I would suggest you add a LinearItemsLayout which would give you some control over how these items look:

 <CollectionView.ItemsLayout>  
 <LinearItemsLayout ItemSpacing="5" Orientation="Horizontal" />
 </CollectionView.ItemsLayout>

Let me know in case if you face any issues!



来源:https://stackoverflow.com/questions/59103182/xamarin-forms-collectionview-is-not-working-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!