I am unable to set the particular page in Carousel view

BadamSujithKumarBadamSujithKumar ✭✭USMember ✭✭
edited July 2018 in Xamarin.Forms

Hi, I am implemented the Carousel view using https://blog.xamarin.com/flip-through-items-with-xamarin-forms-carouselview/. And I am able to implement the Carousel,
**But My Problem I want to set the startup page while opening (I mean when page is loaded the carousel view should show 5th page or 4th index) **

Plugin Used
Xamarin.Forms.CarouselView (2.3.0-pre2)

What I tried
Case 1: I have used Data binding in xaml but it always showing first page or index of zero

Case 2: I have used Data binding from .cs file like CarouselView.ItemSource= Images; and CarouselView.Position=5; but is giving unhandled exception i.e, System.DivideByZeroException: Attempted to divide by zero.

Here is the Sample code

Case 1:

            <cv:CarouselView Position="{Binding Position, Mode=TwoWay}" ItemSelected="CarouselAlbumItems_ItemSelected" ItemsSource="{Binding AlbumItems}" x:Name="CarouselAlbumItems">
                <cv:CarouselView.ItemTemplate>
                    <DataTemplate>
                        <Grid  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Image x:Name="imgAlbumItemName" Grid.RowSpan="2" Aspect="AspectFit" IsVisible="{Binding IsImage}" >
                                <Image.Source>
                                    <UriImageSource Uri="{Binding AlbumItemPath}" CacheValidity="14" />
                                </Image.Source>
                            </Image>
                        </Grid>
                    </DataTemplate>
                </cv:CarouselView.ItemTemplate>
            </cv:CarouselView>

Case 2:

    protected  override void OnAppearing()
    {
        base.OnAppearing();
        CarouselAlbumItems.ItemsSource = selectedItem.AlbumItems;
        CarouselAlbumItems.Position = position;            
    }

Best Answer

Answers

  • BadamSujithKumarBadamSujithKumar ✭✭ USMember ✭✭

    And I have observed that position never have the value of zero

  • BillyLiuBillyLiu Xamurai Member, Xamarin Team Xamurai
    edited August 2018

    @BadamSujithKumar

    <cv:CarouselView Position="{Binding Position, Mode=TwoWay}" ItemSelected="CarouselAlbumItems_ItemSelected" ItemsSource="{Binding AlbumItems}" x:Name="CarouselAlbumItems">
    

    Do you bind other value to Position property?

  • AndreiMisiukevich_AndreiMisiukevich_ ✭✭✭✭✭ USMember ✭✭✭✭✭
  • BadamSujithKumarBadamSujithKumar ✭✭ USMember ✭✭

    @BillyLiu Yes I have bind the other value

  • Alamgeer_AshrafAlamgeer_Ashraf Member

    @BadamSujithKumar

    You have to update CarouselView Position inside MainThread.

    eg: protected override void OnAppearing()
    {
    base.OnAppearing();

            // Iphone working but in Android not working "Devide by Zero Exception"
            //CarouselMain.Position = pos;
    
            Device.BeginInvokeOnMainThread(() => {
    
                CarouselMain.Position = pos;
            });
    
        }
    
  • BadamSujithKumarBadamSujithKumar ✭✭ USMember ✭✭
    edited January 31

    @Alamgeer_Ashraf

    Thanks for the reply, I did not check your answer because I had been resolved the problem before your comment. I am able to solved by using latest nuget as suggested by @JGoldberger

Sign In or Register to comment.