Xamarin.ios 如何为MvxCollectionViewController提供页眉或页脚?

Xamarin.ios 如何为MvxCollectionViewController提供页眉或页脚?,xamarin.ios,uicollectionview,xamarin,mvvmcross,Xamarin.ios,Uicollectionview,Xamarin,Mvvmcross,我试图从MvxCollectionViewController中提供页眉和页脚视图,但遇到了问题。通常,对于UICollectionViewController,我会覆盖GetViewForSupplementalElement方法,如下所示: public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind,

我试图从MvxCollectionViewController中提供页眉和页脚视图,但遇到了问题。通常,对于UICollectionViewController,我会覆盖GetViewForSupplementalElement方法,如下所示:

public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
    var someHeaderOrFooterView = (HeaderOrFooterView) collectionView.DequeueReusableSupplementaryView (elementKind, elementId, indexPath);
    return someHeaderOrFooterView;
}
MVXCollectionViewController似乎不像UICollectionViewController那样获得对GetViewForSupplementalElement方法的委托回调


是否有其他方法可以使用MvxCollectionViewController指定CollectionView的页眉和页脚?

标准步骤应该可以工作(它们在此处工作…):

  • 在布局中提供标题的大小

        HeaderReferenceSize = new System.Drawing.SizeF(100, 100),
    
  • 为标头实现一个类

    public class Header : UICollectionReusableView
    {
        UILabel label;
    
        public string Text
        {
            get { return label.Text; }
            set { label.Text = value; SetNeedsDisplay(); }
        }
    
        [Export("initWithFrame:")]
        public Header(System.Drawing.RectangleF frame)
            : base(frame)
        {
            label = new UILabel() { Frame = new System.Drawing.RectangleF(0, 0, 300, 50), BackgroundColor = UIColor.Yellow };
            AddSubview(label);
        }
    }
    
  • 注册那个班级

       CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, headerId); 
    
  • 实现GetViewForSupplementaryElement

    public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
    {
        var headerView = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, headerId, indexPath);
        headerView.Text = "Supplementary View";
        return headerView;
    }
    
  • 刚刚在这里测试了这些步骤,它们在我的示例应用程序中工作(基于)



    旁白>在提供可绑定的补充视图方面存在一个悬而未决的问题--但是这个问题不应该影响基本的集合视图操作。

    我在MvvmCross上也遇到了同样的问题。我可以通过子类化MvxCollectionViewSource类并重写GetViewForSupplementalElement方法来解决这个问题:

    public class MyCollectionViewSource: MvxCollectionViewSource
    {
        public TimelineSource(UICollectionView collectionView, NSString defaultCellIdentifier) : base(collectionView, defaultCellIdentifier)
        {
        }
    
        public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
        {
            var headerView = (MyHeader)collectionView.DequeueReusableSupplementaryView(elementKind, MyHeader.Key, indexPath);
            headerView.Text = "Supplementary View";
            return headerView;
        }
    }
    

    我在使用MvvmCross时遇到了同样的问题,MvxCollectionViewController从不调用

    公共覆盖UICollectionReusableView GetViewForSupplementaryElement

    public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
    {
        var headerView = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, headerId, indexPath);
        headerView.Text = "Supplementary View";
        return headerView;
    }
    
    我用“Perfem_元素”的答案解决了这个问题

    最后在MvxViewController中:

    var source = new MyCollectionViewSource (CollectionView, ListadoCollectionCell.Key);
    

    谢谢你,斯图尔特。非常感谢。我在一个单独的项目中对此进行了测试,该项目实际上只是来自Xamarin的SimpleCollectionView示例的一个副本,并且运行良好。也就是说,我的工作项目仍然存在同样的问题—GetViewForSupplementaryView从未被调用。我必须继续努力追查这个问题。但我可以(再次)确认上述方法有效。再次感谢。