dotnet / dotnet/maui

TapGestureRecognizer doesn't fire Tapped event on child controls

Open
#8,121 13 comments 0 reactions 0 assignees View on GitHub
area-gestures platform/android s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

I added a TapGestureRecognizer to a custom `ContentView ` which contains several other controls. I wondered why the `Tapped` event is not getting fired on Android when I tap on the ContentView. The same code is working on Windows.

I figured out, that when I add the `TapGestureRecognizer ` to a child control which occupies alot of sapce the `Tapped` event gets fired.
From my observation it looks like that the Tapped Event / TapGestureRecognizer will not be forwarded to its child controls.

This bug may be related to #6644 , #7466, #8004

### Steps to Reproduce

1. Create dotnet maui default project
2. Add ContentView
3. Add to the ContentView serveral controls
4. register/add TapGestureRecognizer to ContentView.
5. Tap on it and check if breakpoint gets hit on Tapped event
6. Event is not fired

In the attached project [TapGestureRecognizer_doesnt_fire_Tapped_event_on_child_controls.zip](https://github.com/dotnet/maui/files/8921079/TapGestureRecognizer_doesnt_fire_Tapped_event_on_child_controls.zip) the ContentView contains the following control hierarchy:

- ContentView
- Grid
- Frame (occupies quite a lot of sapce)
- ...

Adding the `tapGestureRecognizer ` directly to the `content` (which is of type ContentView) will not fire the `Tapped` event.
If you comment line `control = ((content as ContentView).Children[0] as Grid).Children[0] as View` in the tapGestureRecognizer
gets added directly on the Frame. Tapping now on the ContentView will fire the `Tapped` event.

```csharp
private void MainPage_Loaded(object sender, EventArgs e)
{
View control = content; //content = ContentView
//control = ((content as ContentView).Children[0] as Grid).Children[0] as View; //Frame
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Parent = control;
tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
control.GestureRecognizers.Add(tapGestureRecognizer);
}
```

### Version with bug

6.0.312

### Last version that worked well

Unknown/Other

### Affected platforms

Android

### Affected platform versions

android

### Did you find any workaround?

Register the TapGestureRecognizer on all sub controls of the ContentView.

### Relevant log output

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by running the attached TapGestureRecognizer_doesnt_fire_Tapped_event_on_child_controls.zip sample and follow MainPage_Loaded, where the recognizer is added to the ContentView. Compare this with registering it on the Frame child and verify the expected behavior on Android; done means tapping the ContentView fires the Tapped event without registering handlers on every child control.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, csharp
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.