Skip to content

Instantly share code, notes, and snippets.

@vividos
Created May 28, 2020 20:14
Show Gist options
  • Select an option

  • Save vividos/d2819d8e3890413593c04e9d622ceee8 to your computer and use it in GitHub Desktop.

Select an option

Save vividos/d2819d8e3890413593c04e9d622ceee8 to your computer and use it in GitHub Desktop.
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Controls.Issues
{
/// <summary>
/// Tests list view that has view cells with an element (in this case a Label) that has a tap
/// gesture recognizer, and the list view also has an ItemTapped handler. When the view cell
/// also has a context action, the ItemTapped handler isn't called anymore.
/// </summary>
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 2180, "ItemTapped is not called when cell item has a TapGestureRecognizer", PlatformAffected.Android)]
public class Issue2180 : ContentPage
{
public Issue2180()
{
var listView = new ListView
{
RowHeight = 40
};
listView.ItemsSource = new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
listView.ItemTapped += ListView_ItemTapped;
listView.ItemTemplate = new DataTemplate(() =>
{
var label1 = new Label
{
FontSize = 16.0
};
label1.SetBinding(Label.TextProperty, ".");
var image = new Image { Source = "coffee.png" };
var label2 = new Label
{
FontSize = 20.0,
Text = "ItemTapped"
};
var gestureRecognizer = new TapGestureRecognizer
{
NumberOfTapsRequired = 1,
Command = new Command(Label_TapGestureRecognizerCommand)
};
image.GestureRecognizers.Add(gestureRecognizer);
var imageView = new ContentView()
{
Content = image
};
var contextMenuItem = new MenuItem
{
Text = "Show details",
Command = new Command(ContextActionShowDetailsCommand)
};
var viewCell = new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 5),
Orientation = StackOrientation.Horizontal,
Children = { label1, imageView, label2 }
},
};
viewCell.ContextActions.Add(contextMenuItem);
return viewCell;
});
Content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { listView }
};
}
void ListView_ItemTapped(object sender, ItemTappedEventArgs args)
{
DisplayAlert("Alert", "List item tapped", "OK");
}
void Label_TapGestureRecognizerCommand()
{
DisplayAlert("Alert", "Tap gesture recognized", "OK");
}
private void ContextActionShowDetailsCommand(object obj)
{
DisplayAlert("Alert", "Context action", "OK");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment