XAMARIN 如何使用由另一个按钮创建的按钮的事件处理程序

XAMARIN How to use the event handler of a button that has been created by another button

所以我制作了一个程序,它有一个“添加到收藏夹”按钮(一种最喜欢的颜色)所以我 select 一种颜色,当我单击“添加”按钮时它会转到事件处理程序(按下)并使用此代码创建一个新按钮:

        Frame frame;
        private void OnAdd(object sender, EventArgs e)
        { 
               parent.Children.Add(frame = new Frame
                {
                    BorderColor = Color.Black,
                    Padding = new Thickness(5),
                    Content = new StackLayout
                    {
                        Orientation = StackOrientation.Horizontal,
                        Children =
                        {
                            new Button
                            {
                                Text="btn",

                            },
                            new Label
                            {
                                Text ="sample text", FontSize=Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                                VerticalOptions = LayoutOptions.Center
                            }
                        }
                    }
                });
         }

问题是我想检测是否也按下了那些创建的按钮,这样我就可以在它们自己的事件处理程序中编写代码。

顺便说一句,这与颜色无关(只是为了说明),更多的是关于“添加到收藏夹”系统。

提前致谢:))

只需设置他的eventHandler即可。

........your code
new Button
  {
      Text="btn",

  }
......

应该改为:

var button=new Button{Text="btn"};
button.Click=OnAdd;

然后将此button添加到您的框架

所以是的,我基本上做了这样的东西而且它很管用:)

        private void OnAdd(object sender, EventArgs e)
        {
            Button btnFav = new Button { Text = "btn"};
            btnFav.Clicked += ONbtnFav;

                parent.Children.Add(frame = new Frame
                {
                    BorderColor = Color.Black,
                    Padding = new Thickness(5),
                    Content = new StackLayout
                    {
                        Orientation = StackOrientation.Horizontal,
                        Children =
                        {
                            btnFav,
                            new Label
                            {
                                Text ="sample text", FontSize=Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                                VerticalOptions = LayoutOptions.Center
                            }
                        }
                    }
                });
         }