Long Press in .NET Maui

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

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.
3,599 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 5,537 Reputation points Microsoft Employee
    2022-06-24T16:54:41.27+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 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.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.