locked
CarouselView doesn't set its position, CurrentItemChanged and PositionChanged don't work either RRS feed

  • Question

  • User393643 posted

    I have a gallery app. There is a grid with several images in it. When a user clicks on the image, it displays the fullscreen version of it using a carousel. The problem is that even though I set the carousel's position to the index of my image in the list, it displays the first image in the carousel everytime. While debugging, I can see that the position is correctly set, but it doesn't display the image correctly.

    I also have issues when sliding in the carousel. The methods "CurrentItemChanged" and "PositionChanged" never trigger.

    Anyone has a solution ?

    Saturday, April 4, 2020 12:04 PM

All replies

  • User382871 posted

    The problem is that even though I set the carousel's position to ... it displays the first image in the carousel everytime. I tested a basic demo about the function and it works fine. And CurrentItemChanged and PositionChanged also work well. <CarouselView x:Name="carouselView" Position="2" ItemsSource="{Binding Monkeys}" CurrentItemChanged="carouselView_CurrentItemChanged" PositionChanged="carouselView_PositionChanged"> <CarouselView.ItemsLayout> <LinearItemsLayout Orientation="Horizontal" SnapPointsType="MandatorySingle"/> </CarouselView.ItemsLayout> <CarouselView.ItemTemplate> <DataTemplate> <StackLayout> ... </StackLayout> </DataTemplate> </CarouselView.ItemTemplate> </CarouselView>

    How did you test CarouselView? Could you posted the related code? You can also refer to the official sample.

    Monday, April 6, 2020 6:47 AM
  • User393643 posted

    He> @Jarvan said:

    The problem is that even though I set the carousel's position to ... it displays the first image in the carousel everytime. I tested a basic demo about the function and it works fine. And CurrentItemChanged and PositionChanged also work well. <CarouselView x:Name="carouselView" Position="2" ItemsSource="{Binding Monkeys}" CurrentItemChanged="carouselView_CurrentItemChanged" PositionChanged="carouselView_PositionChanged"> <CarouselView.ItemsLayout> <LinearItemsLayout Orientation="Horizontal" SnapPointsType="MandatorySingle"/> </CarouselView.ItemsLayout> <CarouselView.ItemTemplate> <DataTemplate> <StackLayout> ... </StackLayout> </DataTemplate> </CarouselView.ItemTemplate> </CarouselView>

    How did you test CarouselView? Could you posted the related code? You can also refer to the official sample.

    Thanks for your answers. I've already seen this sample and I tried it for my project but no positive result.

    Here's how I try to set the carousel's position whenever the user clicks on a ImageButton:

    ``` private void ImgBtn_Clicked(object sender, EventArgs e) { int index = 0;

            //Get the ImageButton pressed
            ImageButton imgButton = (ImageButton)sender;
    
            //Get the ImageButton's source as string
            UriImageSource imgBtnUri = imgButton.Source as UriImageSource;
            string strFileName = imgBtnUri.Uri.AbsoluteUri;
    
            //Get the index of the ImageButton's pressed in the list of sources
            for (int i = 0; i < sources.Count; i++)
            {
                if (sources[i] == strFileName)
                {
                    index = i;
                    break;
                }
            }
    
            grid_images.IsVisible = false;
    
            //Set ItemsSource of the carousel
            carou_slideshow.ItemsSource = sources;
            //Set the carousel's position to the index found
            carou_slideshow.Position = index + 1;
            //carou_slideshow.Position = index;
            carou_slideshow.IsVisible = true;
    
            btn_imgBack.IsVisible = true;
            btn_propeties.IsVisible = true;
        }
    

    ```

    XAML:

    <CarouselView x:Name="carou_slideshow" Grid.Row="1" IsVisible="False"> <CarouselView.ItemTemplate> <DataTemplate> <Image Source="{Binding}"/> </DataTemplate> </CarouselView.ItemTemplate> </CarouselView>

    Have you done something special with the CurrentItemChange and PositionChanged methods?

    Monday, April 6, 2020 5:42 PM
  • User382871 posted

    Have you done something special with the CurrentItemChange and PositionChanged methods? No, here is the demo file, you can refer to the code.

    Wednesday, April 8, 2020 7:28 AM
  • User365995 posted

    This is an Android example ... perhaps the question referred to UWP? I can't get PositionChanged to work in UWP ...(but there again - I've never made anything else in Xamarin work either ...)

    Friday, May 8, 2020 6:37 PM