2014-01-14 15 views
2

我使用Xamarin構建Google Glass應用程序,並且無法讓GestureDetector觸發任何OnGesture事件。以下是我試過到目前爲止:我如何獲得Google Glass手勢與Xamarin合作?

在我的活動:

using Gesture = Android.Glass.Touchpad.Gesture; 
    using GestureDetector = Android.Glass.Touchpad.GestureDetector; 

    private GestureDetector _gestureDetector; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     this._gestureDetector = new GestureDetector(this); 
     this._gestureDetector.SetBaseListener(new GestureListener()); 
    } 

    public override bool OnGenericMotionEvent(MotionEvent e) 
    { 
     if (this._gestureDetector != null) 
     { 
      return this._gestureDetector.OnMotionEvent(e); 
     } 

     return false; 
    } 

的IBaseListener實現:

class GestureListener : GestureDetector.IBaseListener 
{ 
    public bool OnGesture(Gesture gesture) 
    { 

     if (gesture == Gesture.SwipeRight) 
     { 
      // do something on right (forward) swipe 
      return true; 
     } 
     else if (gesture == Gesture.SwipeLeft) 
     { 
      // do something on left (backwards) swipe 
      return true; 
     } 
     return false; 
    } 

    public void Dispose() 
    { 

    } 

    public IntPtr Handle { get; set; } 
} 

我試着設置一個斷點只是OnGesture方法內,但它永遠不會被觸發。我的IBaseListener實現中是否缺少某些東西?

回答

3

事實證明,我需要讓我的聽衆實現擴展爲Java.Lang.Object,如Xamarin文章中提到的關於Android Callable Wrappers。以下是一個完整的示例活動。

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Gesture = Android.Glass.Touchpad.Gesture; 
using GestureDetector = Android.Glass.Touchpad.GestureDetector; 


namespace GlassGestureTest 
{ 
    using Android.Util; 

    using Java.Util.Logging; 

    [Activity(Label = "GlassGestureTest", MainLauncher = true, Icon = "@drawable/icon")] 
    public class Activity1 : Activity 
    { 

     private GestureDetector _gestureDetector; 

     int count = 1; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button>(Resource.Id.MyButton); 

      button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; 

      this._gestureDetector = new GestureDetector(this); 
      this._gestureDetector.SetBaseListener(new GestureListener()); 
     } 

     public override bool OnGenericMotionEvent(MotionEvent e) 
     { 
      if (this._gestureDetector != null) 
      { 
       return this._gestureDetector.OnMotionEvent(e); 
      } 

      return false; 
     } 
    } 

    // Note - the key part here is to extend Java.Lang.Object in order to properly setup 
    // the IntPtr field and Dispose method required by IBaseListener 
    public class GestureListener : Java.Lang.Object, GestureDetector.IBaseListener 
    { 
     public bool OnGesture(Gesture gesture) 
     { 
      if (gesture == Gesture.SwipeRight) 
      { 
       // do something on right (forward) swipe 
       return true; 
      } 
      else if (gesture == Gesture.SwipeLeft) 
      { 
       // do something on left (backwards) swipe 
       return true; 
      } 
      else if (gesture == Gesture.SwipeDown) 
      { 
       // do something on the down swipe 
       return true; 
      } 
      else if (gesture == Gesture.SwipeUp) 
      { 
       // do something on the up swipe 
       return true; 
      } 

      return false; 
     } 
    } 
} 
+0

這很棒;感謝分享。我想知道如何獲得卡的處理,然後udpate?例如,如果我想更新卡(Tapped(手勢== Tap)時) - 有關如何做到這一點的任何想法?謝謝。 – bahree

+0

@bahree如果您發佈了一些問題並提供更多詳細信息,我可以查看一下。 – elevine

相關問題