如何在 Xamarin 的 Children ImageButton 中触发方法?

How to trigger method in Children ImageButton in Xamarin?

我已经创建了 ImageButton,它是 StackLayout 的子项,我想通过单击它来激活删除方法。我不能使用“点击”,所以我不知道该怎么做。

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

你可以使用 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 
                        }
                    }