我正在尝试使用Xamarin.Forms将OnFling
连接到TextCell
。我相信我已经正确地连接了事件,但我不确定如何将控件的引用传递给侦听器对象?
我基本上想要做以下事情:
public class MyTextCell : TextCell
{
public void UserSwiped (SwipeDirection direction)
{
//Do Foo...
}
}
[assembly: ExportRenderer (typeof(MyTextCell), typeof(MyTextCellRenderer))]
public class MyTextCellRenderer : TextCellRenderer
{
private readonly MyTextCellListener _listener;
private readonly GestureDetector _detector;
public MyTextCellRenderer ()
{
_listener = new MyTextCellListener (MyTextCell); //Here I need to get the reference to the MyTextCell that the renderer is rendering...
_detector = new GestureDetector (_listener);
}
}
public class BincTextCellListener : GestureDetector.SimpleOnGestureListener
{
public MyTextCellListener(MyTextCell cell)
{
Cell = cell;
}
MyTextCell Cell {get; set; }
public override bool OnFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
Cell.UserSwiped(SwipedDirection.SomeDirection);
}
}
This post建议我在覆盖NewElement
时能够从OnElementChanged()
对象获取引用。但是,TextCellRenderer
没有要覆盖的方法。 (相当于GetCellCore()
,但是NewElement
也没有属性?)
是否有人能够建议如何从MyTextCell
访问MyTextCellRenderer
对象?
答案 0 :(得分:0)
GetCellCore的第一个参数是一个Xamarin.Forms.Cell,您应该可以将其转换为自定义的MyTextCell
protected override View GetCellCore (Xamarin.Forms.Cell item, View convertView, ViewGroup parent, Context context)