public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) { using ( _trace.WriteEventActivity( TraceProvider.ListViewBaseSource_GetCellStart, TraceProvider.ListViewBaseSource_GetCellStop ) ) { // Marks this item to be materialized, so its actual measured size // is used during the calculation of the layout. // This is required for paged lists, so that the layout calculation // does not eagerly get all the items of the ItemsSource. UpdateLastMaterializedItem(indexPath); var index = Owner?.XamlParent?.GetIndexFromIndexPath(IndexPath.FromNSIndexPath(indexPath)) ?? -1; var identifier = GetReusableCellIdentifier(indexPath); var listView = (NativeListViewBase)collectionView; var cell = (ListViewBaseInternalContainer)collectionView.DequeueReusableCell(identifier, indexPath); using (cell.InterceptSetNeedsLayout()) { var selectorItem = cell.Content as SelectorItem; if (selectorItem == null || // If it's not a generated container then it must be an item that returned true for IsItemItsOwnContainerOverride (eg an // explicitly-defined ListViewItem), and shouldn't be recycled for a different item. !selectorItem.IsGeneratedContainer) { cell.Owner = Owner; selectorItem = Owner?.XamlParent?.GetContainerForIndex(index) as SelectorItem; cell.Content = selectorItem; if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { this.Log().Debug($"Creating new view at indexPath={indexPath}."); } FrameworkElement.InitializePhaseBinding(selectorItem); // Ensure the item has a parent, since it's added to the native collection view // which does not automatically sets the parent DependencyObject. selectorItem.SetParent(Owner?.XamlParent?.InternalItemsPanelRoot); } else if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { this.Log().Debug($"Reusing view at indexPath={indexPath}, previously bound to {selectorItem.DataContext}."); } Owner?.XamlParent?.PrepareContainerForIndex(selectorItem, index); // Normally this happens when the SelectorItem.Content is set, but there's an edge case where after a refresh, a // container can be dequeued which happens to have had exactly the same DataContext as the new item. cell.ClearMeasuredSize(); } Owner?.XamlParent?.TryLoadMoreItems(index); return(cell); } }