我现在有一个从故事板创建的CollectionView。如果我从模拟器运行应用程序一切正常。如果我在Device上运行相同的应用程序,则永远不会调用Get单元格方法,并且单元格永远不会出现。
这是我在ViewController中的collectionView
//Current MonthyBox CollectionView Settings
CurrentMonthlyBoxCollectionView.PagingEnabled = true;
CurrentMonthlyBoxCollectionView.BackgroundColor = UIColor.Clear;
CurrentMonthlyBoxCollectionView.SetCollectionViewLayout(new UICollectionViewFlowLayout(), true);
((UICollectionViewFlowLayout)CurrentMonthlyBoxCollectionView.CollectionViewLayout).ScrollDirection = UICollectionViewScrollDirection.Horizontal;
((UICollectionViewFlowLayout)CurrentMonthlyBoxCollectionView.CollectionViewLayout).MinimumInteritemSpacing = 10000.0f;
CurrentMonthlyBoxCollectionView.Source = new CurrentMonthlyBoxCollectionViewSource(Vm);
这是我的收集查看来源
class CurrentMonthlyBoxCollectionViewSource : UICollectionViewSource
{
List<MonthlyBoxModel> CurrentBoxList;
DashboardViewModel DashboardModel;
public CurrentMonthlyBoxCollectionViewSource(List<MonthlyBoxModel> CurrentBoxList)
{
this.CurrentBoxList = CurrentBoxList;
}
public CurrentMonthlyBoxCollectionViewSource(DashboardViewModel model)
{
this.DashboardModel = model;
}
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return 1;
}
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
DashboardModel.NavigateToNextPage(DashboardModel.Model);
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
//Create a new cell
CurrentMonthlyBoxCollectionViewCell cell = (CurrentMonthlyBoxCollectionViewCell)collectionView.DequeueReusableCell("CurrentMonthlyBoxCollectionViewCell", indexPath);
cell.setValuesForCurrentCell(DashboardModel);
return cell;
}
[Export("collectionView:layout:sizeForItemAtIndexPath:")]
public virtual SizeF SizeForItemAtIndexPath(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
return new SizeF((float)collectionView.Frame.Width, 100f);
}
}
}
我不知道出了什么问题
更新:
根据xamarin文件&#34;代表&#34;和&#34;数据源&#34;两者都使用&#34; source&#34; 但是,当我们使用流布局并选择为集合视图单元格选择自定义大小时。我们必须从UICollectionViewFlowLayout继承 并从UICollectionViewFlowLayout句柄设置委托给类 所有委托相关代码到该类。
简而言之,我的集合视图代码如下所示
CurrentMonthlyBoxCollectionView.PagingEnabled = true;
CurrentMonthlyBoxCollectionView.BackgroundColor = UIColor.Clear;
CurrentMonthlyBoxCollectionView.SetCollectionViewLayout(new UICollectionViewFlowLayout(), true);
((UICollectionViewFlowLayout)CurrentMonthlyBoxCollectionView.CollectionViewLayout).ScrollDirection = UICollectionViewScrollDirection.Horizontal;
((UICollectionViewFlowLayout)CurrentMonthlyBoxCollectionView.CollectionViewLayout).MinimumInteritemSpacing = 10000.0f;
CurrentMonthlyBoxCollectionView.Source = new CurrentMonthlyBoxCollectionViewSource(Vm);
**CurrentMonthlyBoxCollectionView.Delegate = new CurrentMonthlyBoxCollectionViewDelegate(Vm);**
我添加了新课程
internal class CurrentMonthlyBoxCollectionViewDelegate : UICollectionViewDelegateFlowLayout
{
DashboardViewModel DashboardModel;
public CurrentMonthlyBoxCollectionViewDelegate(DashboardViewModel DashboardModel)
{
this.DashboardModel = DashboardModel;
}
public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
return new CGSize((float)(collectionView.Frame.Width), (float)(collectionView.Frame.Height));
}
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
DashboardModel.NavigateToNextPage(DashboardModel.Model);
}
}
这足以解决问题,并且不要忘记从collectionViewSource
中删除以下方法[Export("collectionView:layout:sizeForItemAtIndexPath:")]
public virtual SizeF SizeForItemAtIndexPath(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
return new SizeF((float)collectionView.Frame.Width, 100f);
}