How to repeat an action pressing a button

Paolo Mossa 161 Reputation points
2022-02-13T21:59:14.55+00:00

Hi guys. I need that an action must be repeated when a button in pressed till it is realased. How can do this?
There is an API for this task? On under Nuget?

I beg the reader not to judge my english.

Thanks in advanced for any reply.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,486 Reputation points Microsoft Vendor
    2022-02-14T03:14:39.673+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I need that an action must be repeated when a button in pressed till it is realased. How can do this?

    You need to execute this operation by two steps.

    Step1: monitor Button's Press and Release event.

    You can achieve it by Buttons' custom renderer. then expose the underlying TouchDown and TouchUp events.

    I create a custom button in Forms project with event handling.

       public class MyButton : Button  
           {  
               public event EventHandler ButtonPressed;  
               public event EventHandler ButtonReleased;  
         
               public virtual void OnPressed()  
               {  
                   ButtonPressed?.Invoke(this, EventArgs.Empty);  
               }  
         
               public virtual void OnReleased()  
               {  
                   ButtonReleased?.Invoke(this, EventArgs.Empty);  
               }  
           }  
    

    Then create the Custom Renderer on each Platform.

    For Android.

       [assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]  
       namespace ButtonClickAndRelease.Droid  
       {  
           internal class MyButtonRenderer : ButtonRenderer  
           {  
               public MyButtonRenderer(Context context) : base(context)  
               {  
               }  
               protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)  
               {  
                   base.OnElementChanged(e);  
         
                   var customButton = e.NewElement as MyButton;  
         
                   var thisButton = Control as Android.Widget.Button;  
                   thisButton.Touch += (object sender, TouchEventArgs args) =>  
                   {  
                       if (args.Event.Action == MotionEventActions.Down)  
                       {  
                           customButton.OnPressed();  
                       }  
                       else if (args.Event.Action == MotionEventActions.Up)  
                       {  
                           customButton.OnReleased();  
                       }  
                   };  
               }  
         
           }  
       }  
    

    For iOS.

       [assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]  
       namespace ButtonClickAndRelease.iOS  
       {  
           internal class MyButtonRenderer: ButtonRenderer  
           {  
               protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)  
               {  
                   base.OnElementChanged(e);  
         
                   var customButton = e.NewElement as MyButton;  
         
                   var thisButton = Control as UIButton;  
                   thisButton.TouchDown += delegate  
                   {  
                       customButton.OnPressed();  
                   };  
                   thisButton.TouchUpInside += delegate  
                   {  
                       customButton.OnReleased();  
                   };  
               }  
           }  
       }  
    

    In the end, we can consuming the Custom Control in Forms.

       <?xml version="1.0" encoding="utf-8" ?>  
       <ContentPage   
                ...  
                     xmlns:local="clr-namespace:ButtonClickAndRelease"  
                   ....>  
         
          ...  
         
               <local:MyButton Text="test" ButtonPressed="MyButton_ButtonPressed" ButtonReleased="MyButton_ButtonReleased"></local:MyButton>  
          ...  
         
       </ContentPage>  
    

    We need to add ButtonPressed and ButtonReleased in the layout's background code.

       private void MyButton_ButtonPressed(object sender, EventArgs e)  
               {  
                     
                   // start your timer with _timer.Start();  
               }  
         
               private void MyButton_ButtonReleased(object sender, EventArgs e)  
               {  
                // stop your timer with   _timer.Stop();  
               }  
    

    Step2: Execute your action repeatedly.

    You can use System.Timers.Timer to execute your action repeatedly, please try to use this examples code.

    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.

    0 comments No comments

0 additional answers

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