Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - Button Pressed & Released Event

I want to my event to trigger when button pressed and released, but I can only find Click event in Xamarin.Forms.

I believe there must be some work around to get this functionality. My basic need is to start a process when button is pressed and stop when released. It seems to be a very basic feature but Xamarin.Forms doesn't have it right now.

I tried TapGestureRecognizer on button, but button is firing only click event.

MyButton.Clicked += (sender, args) =>
{
  Log.V(TAG, "CLICKED");
};

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    Log.V(TAG, "TAPPED");
};
MyButton.GestureRecognizers.Add(tapGestureRecognizer);

Keep in mind that I need those events to be working in Android and iOS togather.

like image 714
umair.ali Avatar asked Aug 06 '16 10:08

umair.ali


1 Answers

Since Xamarin.Forms 2.4.0, The events Pressed and Released are offered out of the box (see PR).

Note: in order to achieve a Walkie Talkie effect you might want to use Device.BeginInvokeOnMainThread (or via Prism's IDeviceService) to invoke consequent actions so the Released event will get called, otherwise the UI thread might be blocked.
Alternatively, you can declare the event handlers as async and await your invocations, to keep the UI thread unoccupied.

like image 98
Shimmy Weitzhandler Avatar answered Oct 21 '22 16:10

Shimmy Weitzhandler