Reputation: 199
I'm trying to implement a carousel view that, when a user selects an image from a list, the Carousel View shows that image from the selected index on start.
My Attempt:
In my code I'm setting the Position field to the selected index, but its not working for me. I also don't see any other fields associated with CarouselView that would move to a selected index from an index number or matching object (unless I missed something).
Heres my code so far:
public partial class ImagePreview : ContentPage
{
int index;
public ImagePreview(Picture photo, int i)
{
InitializeComponent();
this.index = i;
photo_carousel.Position = index;
photo_carousel.ItemsSource = App.rug.photos;
}
}
Heres the backend:
<ContentPage.Content>
<cv:CarouselView x:Name="photo_carousel">
<cv:CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Grid.RowSpan="2" Aspect="AspectFill" Source="{Binding ImageUri}"/>
<StackLayout Grid.Row="1" BackgroundColor="#80000000" Padding="12">
<Label TextColor="White" Text="{Binding Date}" FontSize="16" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/>
<Label TextColor="White" Text="{Binding Length}" FontSize="16" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/>
</StackLayout>
</Grid>
</DataTemplate>
</cv:CarouselView.ItemTemplate>
</cv:CarouselView>
</ContentPage.Content>
Upvotes: 1
Views: 3714
Reputation: 11
Instead of:
photo_carousel.Position = index;
photo_carousel.ItemsSource = App.rug.photos;
Try with:
photo_carousel.ItemsSource = App.rug.photos;
photo_carousel.Position = index;
Every time you set the ItemSource, Position will be set to default value of 0
Upvotes: 1
Reputation: 18799
There is currently a bug in the Xamarin.Forms Carousel View on iOS
where it does not scroll to the position on initial load. This is a bug in the library Xamarin have provided.
The pull request which fixes this can be found here: https://github.com/xamarin/Xamarin.Forms.CarouselView/pull/9
They have planned to make the CarouselView
stable by February 2017:
CarouselView v1 Stable - Feature CarouselView was introduced at Xamarin Evolve 2016 and has been in prerelease ever since. v1 brings stability and performance improvements.
Upvotes: 1