GestureDetector不起作用

时间:2015-07-02 18:59:18

标签: android xamarin

我有以下实现来检测用户手势,但它不起作用。

public class CSViewController : Java.Lang.Object, GestureDetector.IOnGestureListener
{
   GestureDetector gestureDetector = null;
   View myView;
   public CSViewController (Context context, View myView) 
    {   
        this.context = context;
        this.myView = myView;
        //myView.Touch += async (object sender, View.TouchEventArgs e) => Console.WriteLine("touch");
        gestureDetector = new GestureDetector(myView.Context,this);
   }

   public bool OnScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
   {
     Console.WriteLine("onscroll");
     return false;
   }
}

但是,出于测试目的,我编写了以下代码,它可以工作。

myView.Touch += async (object sender, View.TouchEventArgs e) => Console.WriteLine("touch");

在实施GestureDetector时,我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

以下是解决方案:

我添加了以下代码并且它有效

myView.Touch += (object sender, View.TouchEventArgs e) => _gestureDetector.OnTouchEvent(e.Event);

以下是解决方案:

public class CSViewController : Java.Lang.Object, GestureDetector.IOnGestureListener
{
   GestureDetector gestureDetector = null;
   View myView;
   public CSViewController (Context context, View myView) 
    {   
        this.context = context;
        this.myView = myView;
        myView.Touch += (object sender, View.TouchEventArgs e) => _gestureDetector.OnTouchEvent(e.Event);
        gestureDetector = new GestureDetector(myView.Context,this);
   }

   public bool OnScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
   {
     Console.WriteLine("onscroll");
     return false;
   }
}