简体   繁体   English

如何在 carouselView 中反映 CarouselView.ItemsSource 中所做的更改? (Xamarin 表单)

[英]How to reflect changes made in CarouselView.ItemsSource in the carouselView? (Xamarin Forms)

I was working on my Xamarin.Forms app, trying to make the Items inside my scrollview change their TransitionX property when scrolling, the problem is that once the scrollview has been shown the items inside it will not change as the source does unless you reassign the carouselview.ItemSource, but I can't do this because it would be extremely inefficient and the scrolling would be awful.我正在开发我的 Xamarin.Forms 应用程序,试图让我的滚动视图中的项目在滚动时更改它们的 TransitionX 属性,问题是一旦显示了滚动视图,其中的项目将不会像源一样更改,除非您重新分配carouselview.ItemSource,但我不能这样做,因为它会非常低效并且滚动会很糟糕。 So is there any way to reflect the changes made in the source, in the carouselView dynamically?那么有什么方法可以动态地反映在源代码中所做的更改,在 carouselView 中吗?

Here goes my code, I have written comments to make it as clear as possible: CarouselView:这是我的代码,我已经写了注释以使其尽可能清晰:CarouselView:

<MasterDetailPage.Detail>
        <ContentPage Title="title">
            <StackLayout
                x:Name="page">

                <CarouselView
                    x:Name="carousel" 

                    VerticalOptions="StartAndExpand"
                    HorizontalOptions="StartAndExpand"

                    BackgroundColor="Transparent"

                    Scrolled="carousel_Scrolled">

                    <CarouselView.Behaviors>
                        <behaviors:CarouselViewParallaxBehavior ParallaxOffset="100"/>
                    </CarouselView.Behaviors>

                    <CarouselView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid>
//Here goes more content which is irrelevant for this matter
                                </Grid>

                                <Image
                                    Source="{Binding ImageSrc}"
                                    BackgroundColor="Transparent"
                                    HeightRequest="500"

//This is the property I am trying to change when scrolling
                                    TranslationX="{Binding Position}"

                                    VerticalOptions="Center"
                                    HorizontalOptions="Center"
                                    Margin="0,-160,0,0"></Image>

                            </Grid>
                        </DataTemplate>
                    </CarouselView.ItemTemplate>
                </CarouselView>

            </StackLayout>
        </ContentPage>
    </MasterDetailPage.Detail>

My xaml.cs code:我的 xaml.cs 代码:

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : MasterDetailPage
    {
        private List<Color> _backgroundColorscount = new List<Color>();
        private List<Color> _backgroundColors = new List<Color>();
        private List<Product> Items;
        private static double position = 0;

        public HomePage()
        {
            InitializeComponent();

//These are the items of my carouselView
            Items = new List<Product>()
            {
                // Just create some dummy data here for now.
                new Product{Title = "Classic burguer", Price = "124$", ImageSrc = "Burguer.png", StartColor = Color.FromHex("#DF8000"), EndColor = Color.FromHex("#DCD800")},
                new Product{Title = "Classic burguer", Price = "124$", ImageSrc = "Burguer.png", StartColor = Color.FromHex("#15DE00"), EndColor = Color.FromHex("#BADE00")},
                new Product{Title = "Classic burguer", Price = "124$", ImageSrc = "Burguer.png", StartColor = Color.FromHex("#00DEAD"), EndColor = Color.FromHex("#DCD800")}
            };

            carousel.ItemsSource = Items;
        }


        private void carousel_Scrolled(object sender, ItemsViewScrolledEventArgs e)
        {
//Here is what I am trying to do (I wrote it right now to show the problem)
            Items[0].Position = position - 10;
// the position property in Items[0] is changed, but the change is not reflected in the carouselView
        }
    }

My Model class:我的模型类:

    class Product
    {
        #region Bindings
        //Item views 
        public string Title { get; set; }
        public string Price { get; set; }
        public string ImageSrc { get; set; }
        public string Description { get; set; }

        // Gradient colors
        private Color startColor;
        public Color StartColor 
        {
            get
            {
                return startColor;
            }

            set
            {
                startColor = value;
            }
        }

        private Color endColor;
        public Color EndColor 
        {
            get
            {
                return endColor;
            }

            set
            {
                endColor = value;
            } 
        }

        private Color backgroundColor;
        public Color BackgroundColor
        {
            get 
            {
                if (startColor != null && endColor != null)
                    backgroundColor = GetBackGroundColor();

                return backgroundColor;
            }
        }

        //Item Properties 
        private double _position;
        public double Position
        {
            get
            { 
                return _position; 
            }
            set
            { 
                _position = value; 
                OnPropertyChanged(); 
            }
        }

        private double _scale;
        public double Scale
        {
            get { return _scale; }
            set
            {
                _scale = value;
                OnPropertyChanged();
            }
        }
        #endregion

        public Product()
        {
            Scale = 1;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

And that is all, if you need more code or info I will provide it to you as soon as I see your request.这就是全部,如果您需要更多代码或信息,我会在看到您的请求后立即提供给您。 Thank you all for your time, have a good day.谢谢大家的时间,祝你有美好的一天。

Well use bindings to make our life easier:使用绑定让我们的生活更轻松:

In your XAML:在您的 XAML 中:

<ListView ItemsSource="{Binding Items}" ...../>

In your C# side Items will be an observable collection在你的 C# 端 Items 将是一个可观察的集合

public ObservableCollection<Product> Items { get; set; }

Also, you won't need此外,您将不需要

carousel.ItemsSource = Items;

Rest would stay the same now as soon as you make any changes to the collection the carousel should change too.只要您对集合进行任何更改,轮播也应更改,其余部分将保持不变。

Note: All properties of the Product class would need to notify on property changed.注意: Product 类的所有属性都需要在属性更改时通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM