Lukas Planespotter
Lukas Planespotter

Reputation: 39

How to trigger method in Children ImageButton in Xamarin?

I have created ImageButton, children of StackLayout and i want to activate delete method by click on it. I cant use "Clicked" so i dont know how to do that.

Content = new StackLayout
                    {
                        Children =
                        {                                
                            new ImageButton {Source = "/drawable/delete", HorizontalOptions = LayoutOptions.End, HeightRequest = 60, BackgroundColor = Color.Red, Padding = new Thickness(20,-5), CornerRadius = 45}
                        }
                    }

Upvotes: 0

Views: 129

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

You could use TapGestureRecognizer


var imagebutton = new ImageButton {Source = "/drawable/delete", HorizontalOptions = LayoutOptions.End, HeightRequest = 60, BackgroundColor = Color.Red, Padding = new Thickness(20,-5), CornerRadius = 45} 

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
imagebutton .GestureRecognizers.Add(tapGestureRecognizer);


Content = new StackLayout
                    {
                        Children =
                        {                                
                            imagebutton 
                        }
                    }

Upvotes: 1

Related Questions