You will have to design a custom renderer in order to achieve this.
In your Core/PCL project create a button class :
public class CustomButton : Button
{
public event EventHandler OnPressed;
public event EventHandler OnReleased;
public void OnPressed()
{
OnPressed?.Invoke(this, EventArgs.Empty);
}
public void OnReleased()
{
OnReleased?.Invoke(this, EventArgs.Empty);
}
}
Then in your iOS and Android create the Renderer :
Eg: Android :
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace yourproject.Droid.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customRendererButton = e.NewElement as CustomButton;
var control = Control as Android.Widget.Button;
control.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Up)
{
customRendererButton.OnReleased();
}
else if (args.Event.Action == MotionEventActions.Down)
{
customRendererButton.OnPressed();
}
};
}
}
}
iOS :
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace yourproject.iOS.Renderer
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
var customRendererButton = e.NewElement as CustomButton;
var control = Control as UIButton;
control.TouchDown += delegate
{
customRendererButton.OnPressed();
};
thisButton.TouchUpInside += delegate
{
customRendererButton.OnReleased();
};
}
}
}
Then in your xaml file, link the button to custombutton :
define namespace as custom in your main tag of xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:custom="clr-namespace:yourproject.Core.namespaceofCustomButton;assembly=yourproject.Core">
then,
<custom:CustomButton x:Name="CustomButton"
Then in UI you can access as below :
CustomButton.OnPressed += (sender, args) =>
{
//Your code
};
CustomButton.OnReleased += (sender, args) =>
{
//Your code.
};
Hope this helps!!