question

jfrasch-3601 avatar image
0 Votes"
jfrasch-3601 asked WenyanZhang-MSFT commented

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.

dotnet-maui
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

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://docs.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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My question was related to .NET Maui. You say "this will probably be in .NET Maui" do you know when? It is not in the current GA.

0 Votes 0 ·

You could check Rob's answer, it is expected to be implemented in MAUI Community Toolkit v1.1.

0 Votes 0 ·
RobCaplan avatar image
0 Votes"
RobCaplan answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.