Xamarin.Forms 中的 CollectionView 使用自定义呈现器

本文关键字:自定义 Forms 中的 CollectionView Xamarin | 更新日期: 2023-09-27 18:35:31

>我有 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>

收藏视图.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();
            }
        }
    }
}

收藏视图源.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"平台有点陌生

我得到的最终输出是:iOS模拟器中上述程序的输出

Xamarin.Forms 中的 CollectionView 使用自定义呈现器

它不会像你说的那样产生蓝屏。继承自ContentView的第一个问题:

public class CollectionView : ContentView

不要将ContentView用于渲染器中的主要逻辑的"空"视图。需要设置Content属性。否则将跳过此控件的呈现。尝试从类继承View CollectionView

其次,我更改了渲染器:

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

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

UICollectionViewLayout 类是一个抽象基类,您 子类,用于生成集合视图的布局信息。

但是苹果为我们提供了现成的实现 - UICollectionViewFlowLayout

希望这有帮助