gpt4 book ai didi

xamarin - 有没有办法使用 Xamarin.Forms 识别屏幕上的短按或长按?

转载 作者:行者123 更新时间:2023-12-02 17:54:58 25 4
gpt4 key购买 nike

我有一个应用程序可以响应屏幕上的短按。我通过添加手势识别器来做到这一点。

有没有办法让它响应短按或长按并让它们调用不同的方法?

最佳答案

您将为此实现渲染器。对于 iOS,您可以使用 UILongPressGestureRecognizer 来检测长按操作,而对于 Android,您可以使用 GestureDetector 来执行相同的操作。

表单控制

public class CustomView : ContentView
{
public event EventHandler<EventArgs> LongPressEvent;

public void RaiseLongPressEvent()
{
if (IsEnabled)
LongPressEvent?.Invoke(this, EventArgs.Empty);
}
}

iOS 渲染器

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.iOS
{
public class CustomViewRenderer : ViewRenderer<CustomView, UIView>
{
UILongPressGestureRecognizer longPressGestureRecognizer;
protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
{
longPressGestureRecognizer = longPressGestureRecognizer ??
new UILongPressGestureRecognizer(() =>
{
Element.RaiseLongPressEvent();
});

if (longPressGestureRecognizer != null)
{
if (e.NewElement == null)
{
this.RemoveGestureRecognizer(longPressGestureRecognizer);
}
else if (e.OldElement == null)
{
this.AddGestureRecognizer(longPressGestureRecognizer);
}
}
}
}
}

Android 渲染器

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.Droid
{
public class CustomViewRenderer : ViewRenderer<CustomView, Android.Views.View>
{
private CustomViewListener _listener;
private GestureDetector _detector;

public CustomViewListener Listener
{
get
{
return _listener;
}
}

public GestureDetector Detector
{
get
{
return _detector;
}
}

protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
{
base.OnElementChanged(e);

if (e.OldElement == null)
{
GenericMotion += HandleGenericMotion;
Touch += HandleTouch;

_listener = new CustomViewListener(Element);
_detector = new GestureDetector(_listener);
}
}

protected override void Dispose(bool disposing)
{
GenericMotion -= HandleGenericMotion;
Touch -= HandleTouch;

_listener = null;
_detector?.Dispose();
_detector = null;

base.Dispose(disposing);
}

void HandleTouch(object sender, TouchEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}

void HandleGenericMotion(object sender, GenericMotionEventArgs e)
{
_detector.OnTouchEvent(e.Event);
}
}

public class CustomViewListener : GestureDetector.SimpleOnGestureListener
{
readonly CustomView _target;

public CustomViewListener(CustomView s)
{
_target = s;
}

public override void OnLongPress(MotionEvent e)
{
_target.RaiseLongPressEvent();

base.OnLongPress(e);
}
}
}

使用示例

<local:CustomView LongPressEvent="Handle_LongPress" />

代码隐藏

void Handle_LongPressEvent(object sender, System.EventArgs e)
{
//handle long press event here
}

您还可以在上面自定义添加命令,使其对 MVVM 更加友好。

您可以引用this link有关手势识别器的更多详细信息。

关于xamarin - 有没有办法使用 Xamarin.Forms 识别屏幕上的短按或长按?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46845595/

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