I am trying to get long touch gestures into my xamarin app. I have a view where a tap brings you to an edit screen & a long touch reveals an options menu. I followed this guide on SO about implementing such a thing. The item I want to be long touchable is a Frame
, so I wrote an extension for Frame
. Here is this class:
public class FrameWithLongTouchGesture : Frame
{
public FrameWithLongTouchGesture()
{
}
public EventHandler LongPressActivated;
public void HandleLongPress(object sender, EventArgs e)
{
//Handle LongPressActivated Event
EventHandler eventHandler = this.LongPressActivated;
eventHandler?.Invoke((object)this, EventArgs.Empty);
}
}
As you can see I have added an event handler to this object. Now I then went about implementing a custom renderer for each platform, I started with iOS (since I am an iOS dev). Worked absolutely no problem, took 5 minutes to get working. So now I've come round to android, this should be even easier since the post I linked earlier shows you how to implement the renderer in android... great!....
Not so great :( No long touch event is handled AT ALL with the exact code in the post. I have set breakpoints, tried to write to the console but the gesture event handler is never fired. I can even see that the phone receives a touch down event because it prints to the console when I run it on my test device. I have absolutely no idea why android isn't letting me recognise this gesture, I have also played around with androids GestureDetector
but that never fired either. Here is my android renderer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Content;
using Android.Views;
using Android.Widget;
using LongTouchGestureDemo;
using LongTouchGestureDemo.Droid;
[assembly: ExportRenderer(typeof(FrameWithLongTouchGesture), typeof(FrameWithLongTouchGestureRenderer))]
namespace LongTouchGestureDemo.Droid
{
public class FrameWithLongTouchGestureRenderer : FrameRenderer
{
FrameWithLongTouchGesture view;
//GestureDetector gesture;
public FrameWithLongTouchGestureRenderer(Context context) : base(context)
{
//gesture = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener());
this.LongClick += (object sender, LongClickEventArgs e) => {
view.HandleLongPress(sender, e);
};
}
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
view = e.NewElement as FrameWithLongTouchGesture;
}
}
}
}
This is really frustrating because I cannot seem to implement core functionality into the android app. It doesn't help that I have no experience developing android, it doesnt seem as easy to implement gestures in droid as it does in iOS unfortunately :/
All help and suggestions are welcomed! Thanks