自定义渲染器中的触摸事件 [XAMARIN]
我正在重写 Xamarin 内置的 OnTouchEvent。我需要用户创建的坐标和操作。仅当我在屏幕上移动手指时,它才能正确发出“移动”和“向上”操作。
然而,它并没有: 1,发射任何东西,如果我不移动手指,只点击一次(应该是1次向下和1次向上事件)。 2、当我移动手指时发出向下动作。
XAML:
<renderers:CustomCollectionView
x:Name="ListItemsContaner"
IsGrouped="True"
ItemsSource="{Binding GroupedItems}">
<CollectionView.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnClicked"></TapGestureRecognizer>
</CollectionView.GestureRecognizers>
...
</renderers:CustomCollectionView>
这是渲染器的代码
using Android.Views;
using AppTourism.Droid.Renderers;
using AppTourism.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomCollectionView), typeof(CustomCollectionViewRenderer))]
namespace AppTourism.Droid.Renderers
{
public class CustomCollectionViewRenderer : CollectionViewRenderer, ICustomCollectionViewRenderer
{
public CustomCollectionViewRenderer(Android.Content.Context context) : base(context) { }
public override bool OnTouchEvent(MotionEvent e)
{
System.Console.WriteLine(e.Action);
System.Console.WriteLine(e.RawX + " " + e.RawY);
return base.OnTouchEvent(e);
}
}
}
I am overriding Xamarin's built in OnTouchEvent. I need the coordinates and the actions, the user creates. It properly emits Move, and Up actions, only if I move my finger on the screen.
It does not however:
1, emit anything, if I don't move my finger, only tap once (should be 1 Down and 1 Up event).
2, emit Down action, when I move my finger.
The XAML:
<renderers:CustomCollectionView
x:Name="ListItemsContaner"
IsGrouped="True"
ItemsSource="{Binding GroupedItems}">
<CollectionView.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnClicked"></TapGestureRecognizer>
</CollectionView.GestureRecognizers>
...
</renderers:CustomCollectionView>
Here's the renderer's code
using Android.Views;
using AppTourism.Droid.Renderers;
using AppTourism.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomCollectionView), typeof(CustomCollectionViewRenderer))]
namespace AppTourism.Droid.Renderers
{
public class CustomCollectionViewRenderer : CollectionViewRenderer, ICustomCollectionViewRenderer
{
public CustomCollectionViewRenderer(Android.Content.Context context) : base(context) { }
public override bool OnTouchEvent(MotionEvent e)
{
System.Console.WriteLine(e.Action);
System.Console.WriteLine(e.RawX + " " + e.RawY);
return base.OnTouchEvent(e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎不仅在 CollectionView 上存在错误,而且在 xamarin 中的其他视图上也存在错误,就像这个问题所说:
https://github.com/xamarin/Xamarin.Forms/issues/7159
解决方法是使用 TapGestureRecognizer 代替:
编辑:
您可以覆盖 DispatchTouchEvent
It seems a bug on not only collectionview but also other views in xamarin, like this issue said:
https://github.com/xamarin/Xamarin.Forms/issues/7159
The workaround is use TapGestureRecognizer instead:
EDIT:
You can override the DispatchTouchEvent