There is a proposal to include this in the MAUI Community Toolkit that is expected to be implemented for v1.1 of that toolkit.
See https://github.com/CommunityToolkit/Maui/issues/86
Long Press in .NET Maui
How do you handle a long press (long tap) in .NET Maui? I do not see a gesture for this.
-
Rob Caplan - MSFT 5,537 Reputation points Microsoft Employee
2022-06-24T16:54:41.27+00:00
1 additional answer
Sort by: Most helpful
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,471 Reputation points Microsoft Vendor
2022-06-24T07:41:12.73+00:00 Hello,
You could try to find the handler of the MAUI control, and get
handler.PlatformView
which is a native control. After that, you can invoke native control methods, and subscribe to native control events such as the gesture event, please check Customize .NET MAUI controls with handlers and refer to the following code:Customize a control with a mapper(my control is imageview, you can replace ImageHandler)
Microsoft.Maui.Handlers.ImageHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) => { #if WINDOWS handler.PlatformView.Holding += PlatformView_Holding; #endif #if ANDROID handler.PlatformView.LongClick += PlatformView_LongClick; #endif #if IOS handler.PlatformView.UserInteractionEnabled = true; handler.PlatformView.AddGestureRecognizer(new UILongPressGestureRecognizer(HandleLongClick)); #endif });
Long press event under each platform
#if WINDOWS private void PlatformView_Holding(object sender, Microsoft.UI.Xaml.Input.HoldingRoutedEventArgs e) { //Touch can produce a Holding action, but mouse devices generally can't. //see https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.holding?view=winrt-22621 } #endif #if IOS private void HandleLongClick(UILongPressGestureRecognizer sender) { //do something } #endif #if ANDROID private void PlatformView_LongClick(object sender, Android.Views.View.LongClickEventArgs e) { // do something } #endif
In addition, there is feature request for Xamarin , as jfversluis said "this will probably be in .NET MAUI", you could also create a new one for MAUI at https://github.com/dotnet/maui/issues/new?assignees=&labels=proposal%2Fopen%2Ct%2Fenhancement&template=feature-request.yml
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.