When working with ViewCells, adding ContextAction by itself will result in a successful build and expected functionality. However when both a TapGestureRecognizer and ContextActions is set on the same ViewCell, only the TapGestureRecognizer will respond and ContextActions no longer work.
A previous bug has been reported but apparently resolved:
https://bugzilla.xamarin.com/show_bug.cgi?id=46363
The following is a simple example that demonstrates the issue:
public class TableViewCellIssuePage : ContentPage
{
#region Constructors
public TableViewCellIssuePage()
{
TapGestureRecognizer tap = new TapGestureRecognizer();
ViewCell viewCell = new ViewCell()
{
View = new Label() { Text = "Cell" }
};
viewCell.ContextActions.Add(new MenuItem() { Text = "Never Shows" });
// If you remove this gesture recognizer then the context action will work.
viewCell.View.GestureRecognizers.Add(tap);
this.Content = new StackLayout()
{
Children =
{
new TableView()
{
Root = new TableRoot()
{
new TableSection()
{
viewCell
}
}
}
}
};
}
#endregion
}
Both TapGestureRecognizer and ContextActions should respond when both being added to a single ViewCell.
ContextActions do not respond when adding a TapGestureRecognizer to the same ViewCell.
Microsoft Visual Studio Enterprise 2017
Version 15.5.4
VisualStudio.15.Release/15.5.4+27130.2024
Microsoft .NET Framework
Version 4.7.02556
Installed Version: Enterprise
Xamarin 4.8.0.757 (7f9ec2a)
Xamarin.Android SDK 8.1.3.0 (HEAD/ef47226b7)
Xamarin.iOS and Xamarin.Mac SDK 11.6.1.2 (6857dfc)
NUGET: Xamarin.Forms - 2.5.0.280555
@ammarMheir Are you seeing this behavior on _both_ iOS and Android?
```C#
public Page CreateDefaultMainPage()
{
TapGestureRecognizer tap = new TapGestureRecognizer();
tap.Command = new Command(() =>
{
});
ViewCell viewCell = new ViewCell()
{
View = new Label() { Text = "Cell" }
};
viewCell.ContextActions.Add(new MenuItem() { Text = "Never Shows" });
// If you remove this gesture recognizer then the context action will work.
viewCell.View.GestureRecognizers.Add(tap);
return new ContentPage()
{
Content =
new StackLayout()
{
Margin = new Thickness(0,100,0,0),
Children =
{
new TableView()
{
Root = new TableRoot()
{
new TableSection()
{
viewCell
}
}
}
}
}
};
}
```
also ItemTapped doesnt work when doing this:
https://github.com/xamarin/Xamarin.Forms/issues/2026
there are some news? it exists in 3.1.0-469394-pre1
Does anybody has found an workaround or has a idea for one?
Xam.Forms v3.1.0697729 has same issue.
A quick work-around on Droid is to put an image on right hand side of cell and attach TapGestureRecogniser that triggers same logic as normal cell tap.
@jonkas2211 Did you found a solution?
Adding a GestureRecognizer to the ViewCell worked as a workaround for now.
No @15mgm15, I didnt find a solution
There are some news? This problem is more than two years old (https://bugzilla.xamarin.com/show_bug.cgi?id=46363). It has gone and back several times, version after version. :((
@15mgm15, the ViwCell does not accpet gestures, right? I have tried to find a workaround, but still unsuccessful. :((
A workaround I found for this on Android was to override StartSupportActionMode
and OnSupportActionModeFinished
on my main activity, and use those calls to set a global flag that allows the tap handler to know to ignore the single click event because the context menu is being displayed.
It's kind of horrible, but it does work for my scenario: I'm able to cleanly handle both the long press from context actions and the tap from the gesture recognizer.
This is still an issue in 4.0.0.482894.
Still an issue in 4.4.0.991640
This issue doesn't seem to have had any activity in a long time. We're working on prioritizing issues and resolving them as quickly as we can. To help us get through the list, we would appreciate an update from you to let us know if this is still affecting you on the latest version of Xamarin.Forms, since it's possible that we may have resolved this as part of another related or duplicate issue. If we don't see any new activity on this issue in the next 30 days, we'll evaluate whether this issue should be closed. Thank you!
This is still an issue with Xamarin Forms 4.8.0.1269.
It is exacerbated by https://github.com/xamarin/Xamarin.Forms/issues/7642, which requires a GestureRecognizer in order for long-press to work at all.
The best workaround for all this is to implement IOnLongClickListener.OnLongClick
on a ButtonRenderer, and then call MainActivity.Activity.StartActionMode
and manually add IMenuItem
instances to the IMenu
provided in the ActionMode callback.
This of course requires a lot of Android-specific code. Ideally there would be Xamarin Forms-native ways to make all this work.
Most helpful comment
A workaround I found for this on Android was to override
StartSupportActionMode
andOnSupportActionModeFinished
on my main activity, and use those calls to set a global flag that allows the tap handler to know to ignore the single click event because the context menu is being displayed.It's kind of horrible, but it does work for my scenario: I'm able to cleanly handle both the long press from context actions and the tap from the gesture recognizer.