Xamarin RendererFactory.GetRenderer()无效

时间:2015-11-03 19:22:19

标签: xamarin renderer xamarin-forms

我正在使用Xamarin开发跨平台控件。我的目标是拥有一个可选项目列表。我有一些工作正常的iOS代码:

    private UIView CreateViewWithTemplate(UIPickerView pickerView, nint row)
            {
                // create our content presenter
                DYMOContentPresenter presenter = new DYMOContentPresenter();

                // set its data template to the one from our control
                presenter.ItemTemplate = this._pickerControl.ItemTemplate;

                // set the data to the correct item from the list
                presenter.ObjectContent = this._pickerControl.Items.ElementAt((int)row);

                // and use the xamarin forms renderer factory to get a renderer for the item
                IVisualElementRenderer renderer = RendererFactory.GetRenderer(presenter);

                // tell the renderer the size of the view and 
                renderer.SetElementSize(new Size(pickerView.Frame.Width, ROW_HEIGHT));

                // then get the native view and return


 return renderer.NativeView;
        }

但是,我在Android中实现此功能的尝试无效:

private View CreateViewWithTemplate(int position, View convertView, ViewGroup parent)
{            
    // create content presenter
    DYMOContentPresenter presenter = new DYMOContentPresenter();

    // set its data template to the correct item from the list
    presenter.ItemTemplate = this._pickerControl.ItemTemplate;

    // set data to correct list item
    presenter.ObjectContent = this._pickerControl.Items.ElementAt((int) position);

    // use Xamarin forms renderer factory to get a renderer for the item
    IVisualElementRenderer renderer = RendererFactory.GetRenderer(presenter);

    presenter.Layout(new Xamarin.Forms.Rectangle(0, 0, parent.Width, ROW_HEIGHT));
    renderer.UpdateLayout();
    renderer.Tracker.UpdateLayout();
    renderer.ViewGroup.LayoutParameters = new ViewGroup.LayoutParams(parent.Width, ROW_HEIGHT);
    renderer.ViewGroup.Layout(0, 0, parent.Width, ROW_HEIGHT);
    renderer.ViewGroup.BuildDrawingCache();

    // get native view and return
    return renderer.ViewGroup;
}

我可以看到单个列表视图行,但不会显示任何文本。我可以在注释掉这段代码并用简单的TextView替换它之后看到文本,所以我怀疑渲染器有问题。我也尝试了许多此代码的变体(指定LayoutParams,尝试调整子代的大小等)。似乎没什么用。

0 个答案:

没有答案