Add Command to the Image in cycle

AnMikAnMik USMember ✭✭

Hi. Count of Images inside my custom control depends on some Binding Property. I change count in cycle, but if I set Command to images, it relate to all images. I mean, if I click on one Image - fires all Images.

List<View> holders = new List<View>();

            //генерируем фото-холдеры
            for (int i = 0; i < count; i++)
            {
                Image image = new Image()
                {
                    Source = ImageSource.FromFile(BackgroundImage),
                    HeightRequest = 64,
                    WidthRequest = 64,
                    HorizontalOptions = LayoutOptions.Center,
                    Margin = new Thickness(7),
                    Aspect = Aspect.AspectFit,
                    ClassId = $"{ImageName}{i}",
                };
                image.GestureRecognizers.Add(new TapGestureRecognizer()
                {
                    NumberOfTapsRequired = 1,
                    Command = new Command(async (o) =>
                    {
                        AnchorX = 0.48;
                        AnchorY = 0.48;
                        await this.ScaleTo(0.8, 50, Easing.Linear);
                        await Task.Delay(100);
                        await this.ScaleTo(1, 50, Easing.Linear);

                        OnImageClicked?.Invoke(this, EventArgs.Empty);
                    })
                });

                holders.Add(image);
            }
Tagged:
Sign In or Register to comment.