UICollectionView是黑色的,并没有实例化MvxCollectionViewCell

时间:2014-05-28 21:13:05

标签: ios xamarin.ios uicollectionview mvvmcross

我正在尝试创建一个将绑定到我的viewmodel的UICollectionView。

在我的MvxView上,我有以下内容。

var segmentGridLayout = new UICollectionViewFlowLayout
    {
        ItemSize = new SizeF(240, 400),
        ScrollDirection = UICollectionViewScrollDirection.Vertical
    };

_segmentGrid = new UICollectionView(Frame, segmentGridLayout);
var source = new MvxCollectionViewSource(_segmentGrid, UISegmentCollectionViewCell.Key);
_segmentGrid.RegisterClassForCell(typeof(UISegmentCollectionViewCell), new NSString("SegmentCell"));
_segmentGrid.Source = source;
Add(_segmentGrid)

var set = this.CreateBindingSet<ModuleFragment, ModuleViewModel>();
set.Bind(_segmentGrid).For(p => p.Source).To(vm => vm.ViewSegments);
set.Apply();

this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
this.AddConstraints(
    _segmentGrid.AtTopOf(this, 80),
    _segmentGrid.AtLeftOf(this, 10),
    _segmentGrid.AtRightOf(this, 10),
    _segmentGrid.Height().EqualTo(100),
    _moduleDescription.Below(_segmentGrid, 10),
    _moduleDescription.WithSameLeft(_segmentGrid),
    _moduleDescription.WithSameRight(_segmentGrid)
    );

然后我有一个自定义的Cell

[Register("UISegmentCollectionViewCell")]
public sealed class UISegmentCollectionViewCell : MvxCollectionViewCell
{
    public static readonly NSString Key = new NSString("SegmentCell");
    private readonly UILabel _segmentNumber;
    private readonly UILabel _segmentTitle;

    public UISegmentCollectionViewCell(IntPtr handle)
        : base(string.Empty /* TODO - this isn't really needed - mvx bug */, handle)
    {
        BackgroundView = new UIView();
        SelectedBackgroundView = new UIView();

        ContentView.Layer.BorderColor = CustomUIColor.breathingroom_borders.CGColor;
        ContentView.Layer.BorderWidth = 0.5f;
        ContentView.BackgroundColor = UIColor.White;
        ContentView.Transform = CGAffineTransform.MakeScale(0.8f, 0.8f);

        _segmentNumber = new UILabel(new RectangleF(5, 5, 20, 20))
            {
                TextColor = CustomUIColor.breathingroom_blue,
                Font = UIFont.FromName("HelveticaNeue", 20f),
            };

        _segmentTitle = new UILabel(new RectangleF(5, 15, 50, 20))
        {
            TextColor = CustomUIColor.breathingroom_blue,
            Font = UIFont.FromName("HelveticaNeue", 20f),
        };

        ContentView.AddSubview(_segmentNumber);
        ContentView.AddSubview(_segmentTitle);

        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<UISegmentCollectionViewCell, SegmentViewModel>();
            set.Bind(_segmentNumber).For(p => p.Text).To(vm => vm.Number);
            set.Bind(_segmentTitle).To(vm => vm.Title);
            set.Apply();
        });
    }
}

但是当我运行应用程序时,我希望看到一个UICollectionView的地方,我只是看到一个黑盒子,而在UISegmentCollectionViewCell中,构造函数从未实例化。

enter image description here

这就是CollectionView“假设”看完后的样子。 忽略其他元素;)

enter image description here

1 个答案:

答案 0 :(得分:0)

我没有看到你设置UICollectionViewDataSourceUICollectionViewDelegate,也许这是你想要尝试的。