Xamarin.forms: [Bug] CarouselView SnapPointsAlignment and SnapPointsType doesn't work on iOS

Created on 14 Sep 2019  ·  8Comments  ·  Source: xamarin/Xamarin.Forms

I use Xamarin.Forms 4.3.0.778476-pre1

With this ItemsLayout the CarouselView should be work like this

<CarouselView.ItemsLayout>    
<GridItemsLayout  Orientation="Horizontal"  SnapPointsAlignment="Center" SnapPointsType="Mandatory"/>
</CarouselView.ItemsLayout>

snap

But on my sample i get the following

Screen Recording 2019-09-14 at 11 01 51

I use DataTemplateSelector for different views.

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ShaXam" 
             xmlns:carouselStuff="clr-namespace:ShaXam.UIHelper"
             xmlns:views="clr-namespace:ShaXam.Views" 
             x:Class="ShaXam.MainPage">

        <ContentPage.Resources>
        <ResourceDictionary>
            <carouselStuff:CustomViewSelector x:Key="mySelector"/>
        </ResourceDictionary>
    </ContentPage.Resources>


        <CarouselView ItemsSource="{Binding AvaiableViews}"
                      ItemTemplate="{StaticResource mySelector}">
            <CarouselView.ItemsLayout>
                <GridItemsLayout 
                    Orientation="Horizontal" 
                    SnapPointsAlignment="Center" 
                    SnapPointsType="Mandatory"/>
            </CarouselView.ItemsLayout>
        </CarouselView>

</ContentPage>

    public partial class MainPage : ContentPage
    {

        public List<Type> AvaiableViews { get; set; } // Must have default value or be set before the BindingContext is set.
        private int _position;
        public int Position { get { return _position; } set { _position = value; OnPropertyChanged(); } }

        public MainPage()
        {
            InitializeComponent();

            On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);

            AvaiableViews = new List<Type>() { typeof(RecentView), typeof(ShaxamView), typeof(DiscoverView) };

            BindingContext = this;

        }
    }
    public class CustomViewSelector : DataTemplateSelector
    {
        private DataTemplate view1 = new DataTemplate(typeof(RecentView));
        private DataTemplate view2 = new DataTemplate(typeof(ShaxamView));
        private DataTemplate view3 = new DataTemplate(typeof(DiscoverView));

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            // I don't use 'new' at every time as I do need to retain views status
            Type currentView = item as Type;

            if (currentView == typeof(RecentView))
                return view1;
            else if (currentView == typeof(ShaxamView))
                return view2;
            return view3;
        }
    }

The Android version is working good !!

carouselview iOS 🍎 needs-info ❓ unverified bug

All 8 comments

Thanks for the sample @georgemichailou .
I've been testing and I've seen where us the problem. If you change the Layout from GridItemsLayout to ListItemsLayout:
issue7517

It works as expected. The Carousel only supports ListItemsLayout. In the next Xamarin.Forms version ListItemsLayout changes its name to LinearItemsLayout and it will also be the only Layout that can be used in the Carousel (for now).

Thanks @jsuarezruiz for your time.
I update my solution to the new Xamarin.Forms 4.3.0819712-pre2

I use LinearItemsLayout now, but at the startup I get the following exception (android & ios)

Screenshot 2019-09-21 at 14 12 05

Could you attach a repro sample?. I have updated the sample to 4.3.0819712-pre2 and I cannot reproduce the problem.

7517

I have tried your latest sample (using 4.3.0819712-pre2) and everything works fine. Could you do clean and rebuild and try it again?. @PureWeen can you try the latest sample from George?

@georgemichailou I had the same result as @jsuarezruiz

Can you update to pre3 see how it works for you?

What device are you testing on specifically?

@PureWeen with pre3 is working perfect !! Thanks to all !!

Was this page helpful?
0 / 5 - 0 ratings