【问题标题】:CollectionView in Xamarin.Forms using custom renderersXamarin.Forms 中的 CollectionView 使用自定义渲染器
【发布时间】:2016-05-31 22:57:25
【问题描述】:

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

【问题讨论】:

    标签: c# xaml xamarin.ios xamarin.forms


    【解决方案1】:

    它不会像你说的那样产生蓝屏。 从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 类是一个抽象基类,您可以 子类并用于生成集合视图的布局信息。

    但 Apple 为我们提供了现成的实现 - UICollectionViewFlowLayout

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2017-01-07
      • 1970-01-01
      • 2019-11-02
      • 2014-09-27
      • 2017-11-23
      • 2021-04-28
      相关资源
      最近更新 更多