Long Press in .NET Maui

asked 2022-06-23T11:48:26.493+00:00
jfrasch 21 Reputation points

How do you handle a long press (long tap) in .NET Maui? I do not see a gesture for this.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
874 questions
No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. answered 2022-06-24T07:41:12.73+00:00
    Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 15,576 Reputation points Microsoft Employee

    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.