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.