Xamarin.Forms 中的 CollectionView 使用自定义渲染器

CollectionView in Xamarin.Forms using custom renderers

我有 Xaml 表单,我从中呈现集合视图

XAML

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CustomRenderer_POC;assembly=CustomRenderer_POC" 
             x:Class="CustomRenderer_POC.GalleryPage">
  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
  <local:CollectionView/>
</ContentPage>

CollectionView.CS

using Xamarin.Forms;
namespace CustomRenderer_POC
{
    public class CollectionView : ContentView
    {

    }
}

CollectionViewRender.cs(在 iOS 项目中)

using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using CustomRenderer_POC;
using CoreGraphics;

[assembly: ExportRenderer(typeof(CollectionView), typeof(CollectionViewRenderer))]
namespace CustomRenderer_POC
{
    class CollectionViewRenderer : ViewRenderer<CollectionView, UICollectionView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
        {
            var layout = new UICollectionViewFlowLayout();
            base.OnElementChanged(e);
            if (Control == null)
            SetNativeControl(new UICollectionView(new CGRect(0, 0, 300, 256), new UICollectionViewLayout()));
            if (Control != null)
            {
                int[] a = new int[] {10, 11, 12, 13, 14};
                Control.Source = new CollectionViewSource(a);
                Control.BackgroundColor = UIColor.Blue;
                Control.ReloadData();
            }

        }
    }
}

CollectionViewSource.cs

using System;
using System.Collections.Generic;
using System.Text;
using Foundation;
using UIKit;
using System.Drawing;
using System.Linq;
using CoreGraphics;
using CustomRenderer_POC;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(UICollectionViewSource), typeof(CollectionViewSource))]
namespace CustomRenderer_POC
{
    class CollectionViewSource : UICollectionViewSource
    {
        public Array items = null;
        public CollectionViewSource(Array elements)
        {
            items = elements;
        }
        public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
        {

        }

        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            UICollectionViewCell cell = (UICollectionViewCell)collectionView.DequeueReusableCell("MyCell", indexPath);
            cell.BackgroundColor = UIColor.Red;
            UILabel li = new UILabel(new CGRect(0, 0, 10, 20));
            li.Text = "xyz";
            cell.AddSubview(li);
            return cell;
        }
        public override nint NumberOfSections(UICollectionView collectionView)
        {
            return 1;
        }
        public override nint GetItemsCount(UICollectionView collectionView, nint section)
        {
            return items.Length;
        }
    }

}

现在重点是,

我在所有方法中都添加了断点。

但是,方法 GetCellItemSelected 永远不会被命中。

谁能帮忙...

我关注了很多博客,但没有任何帮助

我对“xamarin”平台有点陌生

我得到的最终输出是:Output of above program in iOS simulator

没有你说的蓝屏。 从 ContentView 继承的第一个问题:

public class CollectionView : ContentView

不要将 ContentView 用于 "empty" 视图,其中是呈现器中的主要逻辑。需要设置Content 属性。或者将跳过此控件的呈现。尝试从 View class.

继承你的 CollectionView

其次,我更改了您的渲染器:

    if (Control == null) {
        SetNativeControl (new UICollectionView (new CGRect (0, 0, 300, 256), new UICollectionViewFlowLayout ()));
        Control.RegisterClassForCell(typeof(UICollectionViewCell), "MyCell");
    }

我把你的 UICollectionViewLayout 改成了 UICollectionViewFlowLayout。 根据 Apple 文档,它是一个抽象基础 class:

The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view.

但是 Apple 为我们提供了随时可用的实现 - UICollectionViewFlowLayout

希望对您有所帮助