xamarin.ios - Xamarin 使用 DataTemplate 形成 UICollectionView

标签 xamarin.ios uicollectionview xamarin.forms datatemplate datatemplateselector

我正在尝试在 Xamarin Forms 中创建一个自定义 View ,在 IOS 中转换为 UICollectionView。

第一件事很简单:

看法:

public class CollectionView : View
{

}

渲染器:
public class CollectionViewRenderer : ViewRenderer<CollectionView, UICollectionView>
{

    protected override void OnElementChanged(ElementChangedEventArgs<CollectionView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            SetNativeControl(new UICollectionView(new CGRect(0, 0, 200, 200), new UICollectionViewFlowLayout()));
        }

        if (e.NewElement != null)
        {
            ...
            Control.Source = new CollectionViewSource(a, this);
            Control.ReloadData();
        }
    }
}

现在我想用 DataTemplates(来自 DataTemplateSelector)提供这个 CollectionView。但我找不到注册类(class)的方法:

从模板中,您可以执行以下操作:
Template.CreateContent();

获取 UI 元素。

但是如何在 collectionView 中注册它以便在 CollectionSource 中出列

例如。:
CollectionView.RegisterClassForCell(typeof(????), "CellId");

最佳答案

希望对你有帮助!!!

自定义控件
GridCollectionView.cs

using System;
using CoreGraphics;
using Foundation;
using UIKit;


namespace MyApp.Forms.Controls
{
    public class GridCollectionView : UICollectionView
    {
        public GridCollectionView () : this (default(CGRect))
        {
        }


        public GridCollectionView(CGRect frm)
            : base(frm, new UICollectionViewFlowLayout())
        {
            AutoresizingMask = UIViewAutoresizing.All;
            ContentMode = UIViewContentMode.ScaleToFill;
            RegisterClassForCell(typeof(GridViewCell), new NSString (GridViewCell.Key));
        }


        public bool SelectionEnable
        {
            get;
            set;
        }


        public double RowSpacing
        {
            get
            {
                return ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumLineSpacing;
            }
            set
            {
                ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumLineSpacing = (nfloat)value;
            }
        }


        public double ColumnSpacing
        {
            get
            {
                return ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumInteritemSpacing;
            }
            set
            {
                ((UICollectionViewFlowLayout)this.CollectionViewLayout).MinimumInteritemSpacing = (nfloat)value;
            }
        }


        public CGSize ItemSize
        {
            get
            {
                return ((UICollectionViewFlowLayout)this.CollectionViewLayout).ItemSize;
            }
            set
            {
                ((UICollectionViewFlowLayout)this.CollectionViewLayout).ItemSize = value;
            }
        }


        public override UICollectionViewCell CellForItem(NSIndexPath indexPath)
        {
            if (indexPath == null)
            {
                //calling base.CellForItem(indexPath) when indexPath is null causes an exception.
                //indexPath could be null in the following scenario:
                // - GridView is configured to show 2 cells per row and there are 3 items in ItemsSource collection
                // - you're trying to drag 4th cell (empty) like you're trying to scroll
                return null;
            }
            return base.CellForItem(indexPath);
        }


        public override void Draw (CGRect rect)
        {
            this.CollectionViewLayout.InvalidateLayout ();


            base.Draw (rect);
        }


        public override CGSize SizeThatFits(CGSize size)
        {
            return ItemSize;
        }
    }
}

渲染器类
GridViewRenderer.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using MyApp.Controls;
using System.Collections.Generic;


[assembly: ExportRenderer (typeof(GridView), typeof(GridViewRenderer))]
namespace MyApp.Controls
{
    public class GridViewRenderer: ViewRenderer<GridView,GridCollectionView>
    {
        private GridDataSource _dataSource;

    public GridViewRenderer ()
        {
        }

        public int RowsInSection(UICollectionView collectionView, nint section)
        {
            return ((ICollection) this.Element.ItemsSource).Count;
        }

        public void ItemSelected(UICollectionView tableView, NSIndexPath indexPath)
        {
            var item = this.Element.ItemsSource.Cast<object>().ElementAt(indexPath.Row);
            this.Element.InvokeItemSelectedEvent(this, item);
        }

        public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var item = this.Element.ItemsSource.Cast<object>().ElementAt(indexPath.Row);
            var viewCellBinded = (this.Element.ItemTemplate.CreateContent() as ViewCell);
            if (viewCellBinded == null) return null;


            viewCellBinded.BindingContext = item;
            return this.GetCell(collectionView, viewCellBinded, indexPath);
        }


        protected virtual UICollectionViewCell GetCell(UICollectionView collectionView, ViewCell item, NSIndexPath indexPath)
        {
            var collectionCell = collectionView.DequeueReusableCell(new NSString(GridViewCell.Key), indexPath) as GridViewCell;


            if (collectionCell == null) return null;


            collectionCell.ViewCell = item;


            return collectionCell;
        }


        protected override void OnElementChanged (ElementChangedEventArgs<GridView> e)
        {
            base.OnElementChanged (e);
            if (e.OldElement != null)
            {
                Unbind (e.OldElement);
            }
            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    var collectionView = new GridCollectionView() {
                        AllowsMultipleSelection = false,
                        SelectionEnable = e.NewElement.SelectionEnabled,
                        ContentInset =  new UIEdgeInsets ((float)this.Element.Padding.Top, (float)this.Element.Padding.Left, (float)this.Element.Padding.Bottom, (float)this.Element.Padding.Right),
                        BackgroundColor = this.Element.BackgroundColor.ToUIColor (),
                        ItemSize = new CoreGraphics.CGSize ((float)this.Element.ItemWidth, (float)this.Element.ItemHeight),
                        RowSpacing = this.Element.RowSpacing,
                        ColumnSpacing = this.Element.ColumnSpacing
                    };


                    Bind (e.NewElement);


                    collectionView.Source = this.DataSource;
                    //collectionView.Delegate = this.GridViewDelegate;


                    SetNativeControl (collectionView);
                }
            }




        }


        private void Unbind (GridView oldElement)
        {
            if (oldElement == null) return;


            oldElement.PropertyChanging -= this.ElementPropertyChanging;
            oldElement.PropertyChanged -= this.ElementPropertyChanged;


            var itemsSource = oldElement.ItemsSource as INotifyCollectionChanged;
            if (itemsSource != null) 
            {
                itemsSource.CollectionChanged -= this.DataCollectionChanged;
            }
        }


        private void Bind (GridView newElement)
        {
            if (newElement == null) return;


            newElement.PropertyChanging += this.ElementPropertyChanging;
            newElement.PropertyChanged += this.ElementPropertyChanged;


            var source = newElement.ItemsSource as INotifyCollectionChanged;
            if (source != null) 
            {
                source.CollectionChanged += this.DataCollectionChanged;
            }
        }


        private void ElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == GridView.ItemsSourceProperty.PropertyName)
            {
                var newItemsSource = this.Element.ItemsSource as INotifyCollectionChanged;
                if (newItemsSource != null) 
                {
                    newItemsSource.CollectionChanged += DataCollectionChanged;
                    this.Control.ReloadData();
                }
            }
            else if(e.PropertyName == "ItemWidth" || e.PropertyName == "ItemHeight")
            {
                this.Control.ItemSize = new CoreGraphics.CGSize ((float)this.Element.ItemWidth, (float)this.Element.ItemHeight);
            }
        }


        private void ElementPropertyChanging (object sender, PropertyChangingEventArgs e)
        {
            if (e.PropertyName == "ItemsSource")
            {
                var oldItemsSource = this.Element.ItemsSource as INotifyCollectionChanged;
                if (oldItemsSource != null) 
                {
                    oldItemsSource.CollectionChanged -= DataCollectionChanged;
                }
            }
        }


        private void DataCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
        {
            InvokeOnMainThread (()=> {
                try 
                {
                    if(this.Control == null)
                        return;


                    this.Control.ReloadData();


                    // TODO: try to handle add or remove operations gracefully, just reload the whole collection for other changes
                    // InsertItems, DeleteItems or ReloadItems can cause
                    // *** Assertion failure in -[XLabs_Forms_Controls_GridCollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:],
                    // BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UICollectionView.m:4324


//                    var indexes = new List<NSIndexPath>();
//                    switch (e.Action) {
//                        case NotifyCollectionChangedAction.Add:
//                            for (int i = 0; i < e.NewItems.Count; i++) {
//                                indexes.Add(NSIndexPath.FromRowSection((nint)(e.NewStartingIndex + i),0));
//                            }
//                            this.Control.InsertItems(indexes.ToArray());
//                            break;
//                        case NotifyCollectionChangedAction.Remove:
//                            for (int i = 0; i< e.OldItems.Count; i++) {
//                                indexes.Add(NSIndexPath.FromRowSection((nint)(e.OldStartingIndex + i),0));
//                            }
//                            this.Control.DeleteItems(indexes.ToArray());
//                            break;
//                        default:
//                            this.Control.ReloadData();
//                            break;
//                    }
                } 
                catch { } // todo: determine why we are hiding a possible exception here
            });
        }

        private GridDataSource DataSource 
        {
            get 
            {
                return _dataSource ?? (_dataSource = new GridDataSource (GetCell, RowsInSection,ItemSelected));
            }
        }


        protected override void Dispose (bool disposing)
        {
            base.Dispose (disposing);
            if (disposing && _dataSource != null)
            {
                Unbind (Element);
                _dataSource.Dispose ();
                _dataSource = null;
            }
        }
    }
}

更多信息Click here for custom classclick here for renderer class

关于xamarin.ios - Xamarin 使用 DataTemplate 形成 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447508/

相关文章:

ios - 迁移到 x64 iOS 后的 Xamarin NetworkReachability SetCallBack 方法问题

ios - Swift:根据单元格数量动态更改单元格高度

xamarin - 方法 'UIKit.UIApplication.Main' 已过时 : Use the overload with 'Type' instead of 'String' parameters for type safety

ios - UITableViewCell 持有 UICollectionView 并点击集合单元格 segue

ios - UICollectionView 内部的 UITableView 处理触摸。

xaml - 更改 Xamarin.Forms 中的文本颜色

c# - Xamarin Forms 后门(Android)

xamarin.forms - 该应用程序无法访问 Xamarin.Forms 中的 iOS 钥匙串(keychain),但可在 Android 中使用

drawing - 为什么 UIGraphics.GetCurrentContext 返回 null?

visual-studio - 无法更改 PCL 库目标框架