How to use Maui TouchEffect?

Will Autio 5 Reputation points
2023-07-17T23:00:09.19+00:00

In Xamarin Forms we could use TouchEffect to determine if there was a touch and what type. The code we used is not working in Maui. Does TouchEffect work in Maui and if so where are some samples of code? If not, what is the recommended way to detect touches?

The following is a snippet of code from Xamarin Forms that worked but does not in Maui.


<Grid Padding="2"
                                  
xct:TouchEffect.LongPressCommand="{Binding TourLongPressCommand,
   Source={RelativeSource AncestorType={x:Type pg:GalleryViewModel}}}"
xct:TouchEffect.LongPressCommandParameter="{Binding .}"
                                  
xct:TouchEffect.Command="{Binding TourPressCommand,
    Source={RelativeSource AncestorType={x:Type pg:GalleryViewModel}}}"
xct:TouchEffect.CommandParameter="{Binding .}"
                       
xct:TouchEffect.PressedScale="1.2">
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,608 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,551 Reputation points Microsoft Vendor
    2023-07-21T01:59:20.0366667+00:00

    Hello,

    a long click could be useful in many places, so wherever it can be added.

    Gird and Frame do not have Handler and cannot use custom renderer in the MAUI. If you want to detect long click for other controls, you can invoke native control methods, and subscribe to native control events such as the gesture event in Handler.

    For example, If I want to detect long click event for Image, we can implement in the Image handler.

     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  
    

    Best Regards,

    Leon Lu


    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.

    1 person found this answer helpful.
    0 comments No comments

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.