I am developing a cross-platform control using Xamarin. My goal is to have a list of selectable items. I have some iOS code that works fine:
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;
}
However, my attempt to implement this in Android is not working:
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;
}
I can see individual list view rows but no text appears within them. I can see text after commenting out this code and replacing it with a simple TextView
, so I suspect there is something wrong with the renderer. I have tried many variants of this code as well (specifying LayoutParams
, attempting to resize the child, etc.). Nothing seems to work.
I have posted this on SO as well.