user6742877
user6742877

Reputation:

How to add TapGestureRecognizer to StackLayout?

So I have this following code:

foreach (var a in abc)
{
   var viewCell = new ViewCell 
   { 
      View = new StackLayout()
      {
         //// I want to add TapGestureRecognizer on this outer stacklayout
         Padding = new Thickness(20, 0, 20, 0),
         HorizontalOptions = LayoutOptions.FillAndExpand,
         Children = { 
            new StackLayout() {
               Orientation = StackOrientation.Horizontal,
               VerticalOptions = LayoutOptions.CenterAndExpand,
               Children = {
                  new StackLayout() {
                     HorizontalOptions = LayoutOptions.StartAndExpand,
                     Children = {
                        new Label { Text = a.Name}}
                      },
                  new StackLayout() {
                     HorizontalOptions = LayoutOptions.EndAndExpand,
                     Orientation = StackOrientation.Horizontal,
                     Children = {
                        new Label { Text = a.Count},
                        new Image { Source = "right1.png" }
                     }
                  }
               }
            }
         }
       }
    };
    tableSection.Add(viewCell);
}

What this code basically do is repeat the rows(ViewCell) of my TableView depending on the number of objects in the abc. I want to add a tap event on the outer (first Stacklayout) but I can't seem to find out how to do it with my how my ViewCell is set up. Anyone has any idea?

Upvotes: 1

Views: 513

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34128

You should create your StackLayout in a variable first and then add a TapGestureRecognizer to it:

foreach (var a in abc)
{
   var stackLayout = new StackLayout()
      {
         //// I want to add TapGestureRecognizer on this outer stacklayout
         Padding = new Thickness(20, 0, 20, 0),
         HorizontalOptions = LayoutOptions.FillAndExpand,
         Children = { 
            new StackLayout() {
               Orientation = StackOrientation.Horizontal,
               VerticalOptions = LayoutOptions.CenterAndExpand,
               Children = {
                  new StackLayout() {
                     HorizontalOptions = LayoutOptions.StartAndExpand,
                     Children = {
                        new Label { Text = a.Name}}
                      },
                  new StackLayout() {
                     HorizontalOptions = LayoutOptions.EndAndExpand,
                     Orientation = StackOrientation.Horizontal,
                     Children = {
                        new Label { Text = a.Count},
                        new Image { Source = "right1.png" }
                     }
                  }
               }
            }
         }
       };

   var tgr = new TapGestureRecognizer();
   tgr.Tapped += (s,e) => OnTgrClicked();
   stackLayout.GestureRecognizers.Add(tgr);

   var viewCell = new ViewCell 
   { 
      View = stackLayout;
    };
    tableSection.Add(viewCell);
}

Upvotes: 1

Related Questions