如何从轮播视图 Xamarin Forms 中的选定索引开始

How to Start At Selected Index in Carousel View Xamarin Forms

我正在尝试实现轮播视图,当用户从列表中选择图像时,轮播视图会在开始时显示所选索引中的图像。

我的尝试:

在我的代码中,我将位置字段设置为选定的索引,但它对我不起作用。我也没有看到任何其他与 CarouselView 关联的字段会从索引号或匹配对象移动到选定的索引(除非我错过了什么)。

到目前为止,这是我的代码:

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;
        }
    }

这是后端:

<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>

目前 iOS 上的 Xamarin.Forms Carousel View 中存在错误,它不会滚动到初始加载时的位置。这是 Xamarin 提供的库中的错误。

可在此处找到解决此问题的拉取请求:https://github.com/xamarin/Xamarin.Forms.CarouselView/pull/9

他们有 planned 到 2017 年 2 月使 CarouselView 稳定:

CarouselView v1 Stable - Feature CarouselView was introduced at Xamarin Evolve 2016 and has been in prerelease ever since. v1 brings stability and performance improvements.

而不是:

photo_carousel.Position = index;
photo_carousel.ItemsSource = App.rug.photos;

试试:

photo_carousel.ItemsSource = App.rug.photos;
photo_carousel.Position = index;

每次设置 ItemSource 时,Position 都会设置为默认值 0