【问题标题】:CarouselView PositionChange event not get called in ContentView page Xamairn.Forms (Android)在 ContentView 页面 Xamarin.Forms (Android) 中未调用 CarouselView 位置更改事件
【发布时间】:2021-09-09 11:09:54
【问题描述】:

目前,我已将项目中的 Xamarin.Forms 版本更新为 5.0.0.2083。之后,我在 CarouselView 控件中遇到了一些奇怪的问题。我在一个地方使用了 Carousel 和 IndicatorView 控件,我面临的问题是 IndicatorView 无法在 Android 设备上运行。

它在 iOS 中运行良好,问题仅来自 Android。为了解决这个问题,我尝试使用 CarouselView 的 PositionChanged 事件,但这也仅适用于 iOS,不适用于 Android。我尝试了很多东西,但都没有奏效。

这是我的代码:

<CarouselView x:Name="creditCardsList" 
            ItemsSource="{Binding CardList}" 
            HorizontalOptions="FillAndExpand" 
            VerticalOptions="FillAndExpand"
            Loop="False" 
            HorizontalScrollBarVisibility="Never"
            Margin="20,0" 
            IsScrollAnimated="True" 
            ItemsUpdatingScrollMode="KeepItemsInView"
            Position="{Binding CarouselPosition}"
            HeightRequest="{StaticResource HeightWidth160}"
            PositionChanged="creditCardsList_PositionChanged"
            IndicatorView="CardIndicatorView">

            <CarouselView.ItemTemplate>
                            <DataTemplate>
                                <!--UI that I want to display-->                                 
                           </DataTemplate>
            </CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView x:Name="CardIndicatorView" 
               VerticalOptions="End" 
               Margin="{DynamicResource MarginPadding00_10_00_10}"
               IndicatorColor="Gray" 
               SelectedIndicatorColor="{StaticResource action_button_color}"
               IndicatorsShape="Square"
               ItemsSource="{Binding CardList}"/>

有没有人知道任何解决方法?我只想在 CarouselView 中滑动项目时更改指示器。任何帮助将不胜感激!

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android carousel


    【解决方案1】:

    我在 Android 上使用 Xamarin.Forms 5.0.0.2083 进行测试,IndicatorView 和事件 PositionChanged 工作正常,请参见下面的屏幕截图。

    更新

    这是我的测试代码

    xaml

      <StackLayout>
            <CarouselView x:Name="creditCardsList" 
                ItemsSource="{Binding CardList}" 
                HorizontalOptions="FillAndExpand" 
                VerticalOptions="FillAndExpand"
                Loop="False" 
                HorizontalScrollBarVisibility="Never"
                Margin="20,0" 
                IsScrollAnimated="True" 
                ItemsUpdatingScrollMode="KeepItemsInView"
                HeightRequest="200"
                PositionChanged="creditCardsList_PositionChanged"
                IndicatorView="CardIndicatorView">
    
                <CarouselView.ItemTemplate>
                    <DataTemplate>
                   
                            <Image Source="dog.png"/>
                      
                    </DataTemplate>
                </CarouselView.ItemTemplate>
            </CarouselView>
            <IndicatorView
                x:Name="CardIndicatorView" 
                   VerticalOptions="End" 
                   Margin="20"
                   IndicatorColor="Gray" 
                   SelectedIndicatorColor="Red"
                   IndicatorsShape="Square"
                   ItemsSource="{Binding CardList}"/>
        </StackLayout>
    

    背后的代码

        public class Model
        {
            public List<string> CardList { get; set; }
        }
    
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class Page1 : ContentPage
        {
            public Page1()
            {
                InitializeComponent();
    
                Model model = new Model();
    
                model.CardList = new List<string> { "1", "2", "3" };
                this.BindingContext = model;
            }
            private void creditCardsList_PositionChanged(object sender, PositionChangedEventArgs e)
            {
    
            }
        }
    

    【讨论】:

    • 如果可能的话,您能否提供一个我们基本的、可重现的项目进行测试?您可以将其上传到您的 github 并在此处附加 repo 链接。
    • 我确实检查过它,如果我从 Constructor 将数据绑定到 ItemSource,它工作正常,但如果我从 OnAppearMethod 做它不工作。
    • 请检查我的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多