Xamarin.Forms. SwipeGesture和ScrollView在Android上无法一起使用

Xamarin.Forms. SwipeGesture和ScrollView在Android上无法一起使用,第1张

概述我将Grid与SwipeGesture和ScrollView一起使用.ScrollView可以很好地工作,但是SwipeGesture不能仅在Android上工作.在iOS中,我没有问题.为什么?请帮帮我<Gridx:Name="grid"><Grid.RowDefinitions><RowDefinitionHeight="*"/><RowDefinit

我将GrID与SwipeGesture和ScrollVIEw一起使用. ScrollVIEw可以很好地工作,但是SwipeGesture不能仅在Android上工作.在iOS中,我没有问题.

为什么?请帮帮我

<GrID x:name="grID">     <GrID.RowDeFinitions>          <RowDeFinition Height="*"/>           <RowDeFinition Height="auto"/>      </GrID.RowDeFinitions>      <ScrollVIEw HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">        ...     </ScrollVIEw> </GrID>

C#:

 var leftSwipeGesture = new SwipeGestureRecognizer { Direction = SwipeDirection.Right }; leftSwipeGesture.Threshold = 50; leftSwipeGesture.Swiped += (sender, e) => Navigation.PopAsync(); grID.GestureRecognizers.Add(leftSwipeGesture);

解决方法:

我基于link解决了此问题.我创建了一个新组件:

public class GestureScrollVIEw : ScrollVIEw{    public event EventHandler Swipeleft;    public event EventHandler SwipeRight;    public voID OnSwipeleft() =>        Swipeleft?.Invoke(this, null);    public voID OnSwipeRight() =>        SwipeRight?.Invoke(this, null);}

AndroID渲染器:

[assembly: ExportRenderer(typeof(GestureScrollVIEw), typeof(GestureScrollVIEWrenderer))]namespace SwipeScrollVIEw.DroID.Platform.renderers{public class GestureScrollVIEWrenderer : ScrollVIEWrenderer{    Readonly CustomGestureListener _Listener;    Readonly GestureDetector _detector;    public GestureScrollVIEWrenderer(Context context) : base(context)    {        _Listener = new CustomGestureListener();        _detector = new GestureDetector(context, _Listener);    }    public overrIDe bool dispatchtouchEvent(MotionEvent e)    {        if (_detector != null)        {            _detector.OntouchEvent(e);            base.dispatchtouchEvent(e);            return true;        }        return base.dispatchtouchEvent(e);    }    public overrIDe bool OntouchEvent(MotionEvent ev)    {        base.OntouchEvent(ev);        if (_detector != null)            return _detector.OntouchEvent(ev);        return false;    }    protected overrIDe voID OnElementChanged(VisualElementChangedEventArgs e)    {        base.OnElementChanged(e);        if (e.NewElement == null)        {            _Listener.OnSwipeleft -= HandleOnSwipeleft;            _Listener.OnSwipeRight -= HandleOnSwipeRight;        }        if (e.oldElement == null)        {            _Listener.OnSwipeleft += HandleOnSwipeleft;            _Listener.OnSwipeRight += HandleOnSwipeRight;        }    }    voID HandleOnSwipeleft(object sender, EventArgs e) =>        ((GestureScrollVIEw)Element).OnSwipeleft();    voID HandleOnSwipeRight(object sender, EventArgs e) =>        ((GestureScrollVIEw)Element).OnSwipeRight();}}

CustomGestureListener:

public class CustomGestureListener : GestureDetector.SimpleOnGestureListener{    static Readonly int SWIPE_THRESHolD = 100;    static Readonly int SWIPE_VELociTY_THRESHolD = 100;    MotionEvent mLastOnDownEvent;    public event EventHandler OnSwipeleft;    public event EventHandler OnSwipeRight;    public overrIDe bool OnDown(MotionEvent e)    {        mLastOnDownEvent = e;        return true;    }    public overrIDe bool OnFling(MotionEvent e1, MotionEvent e2, float veLocityX, float veLocityY)    {        if (e1 == null)            e1 = mLastOnDownEvent;        float diffY = e2.GetY() - e1.GetY();        float diffX = e2.GetX() - e1.GetX();        if (Math.Abs(diffX) > Math.Abs(diffY))        {            if (Math.Abs(diffX) > SWIPE_THRESHolD && Math.Abs(veLocityX) > SWIPE_VELociTY_THRESHolD)            {                if (diffX > 0)                    OnSwipeRight?.Invoke(this, null);                else                    OnSwipeleft?.Invoke(this, null);            }        }        return base.OnFling(e1, e2, veLocityX, veLocityY);    }}

核心:

<ctrl:GestureScrollVIEw x:name="gi"> ... </ctrl:GestureScrollVIEw>gi.Swipeleft += (s, e) =>    displayAlert("Gesture Info", "Swipe left Detected", "OK");                     gi.SwipeRight += (s, e) =>    displayAlert("Gesture Info", "Swipe Right Detected", "OK");
总结

以上是内存溢出为你收集整理的Xamarin.Forms. SwipeGesture和ScrollView在Android上无法一起使用全部内容,希望文章能够帮你解决Xamarin.Forms. SwipeGesture和ScrollView在Android上无法一起使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存