如何获取对Xamarin.Forms中自定义渲染器呈现的控件的引用?

时间:2016-01-02 08:46:00

标签: c# android xamarin

我正在尝试使用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对象?

1 个答案:

答案 0 :(得分:0)

GetCellCore的第一个参数是一个Xamarin.Forms.Cell,您应该可以将其转换为自定义的MyTextCell

protected override View GetCellCore (Xamarin.Forms.Cell item, View convertView, ViewGroup parent, Context context)