gpt4 book ai didi

c# - 如何让 Google Glass 手势与 Xamarin 一起使用?

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:18 24 4
gpt4 key购买 nike

我正在使用 Xamarin 构建 Google Glass 应用程序,但无法让 GestureDetector 触发任何 OnGesture 事件。到目前为止,这是我尝试过的:

在我的 Activity 中:

    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 实现中是否缺少某些内容?

最佳答案

事实证明,我需要让我的监听器实现扩展 Java.Lang.Object,如 Xamarin 关于 Android Callable Wrappers 的文章中所述.下面是一个完整的示例 Activity 。

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;
}
}
}

关于c# - 如何让 Google Glass 手势与 Xamarin 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com