UICollectionView在Xamarin中无法正确更新

时间:2017-08-25 13:07:11

标签: c# ios xamarin uicollectionview

在我的应用中,我有一个非常具体的用途UICollectionView(简称CV),包含4个或更少的单元格。

CV设置为在两个控件之间填充屏幕(顶部控件锚定到父视图的顶部,底部控件锚定到父视图的底部)。我在我的ViewModel中使用ObservableCollection来保存项目,并使用自定义MvxCollectionViewSource(简称CVS)来执行以下操作:

  • 定义两种单元格类型
  • 定义自定义CollectionViewLayout(我使用基于屏幕大小的动态大小单元格,单个单元格可占据屏幕的1/2,减去两侧的8.0填充,以及64.0填充中心,为图标保留)
  • 将项目的索引(indexPath.Row)传递给项目(以备将来显示)
  • 将命令从ViewModel传递到单元格(单元格具有绑定到此命令的“删除”按钮)
  • 覆盖GetItemsCount,如果实际项目数低于设定限制(现在为4),我们总会返回+1(添加按钮)

ViewModel还有一个SelectionChanged的{​​{1}}命令,它所做的就是导航到一个新的ViewModel,它允许编辑所选项目的属性。

我的主要问题是,在所有四个项目都存在之前,项目似乎无法更新(表示主项目中子项目总数的值)。我试过在CVS上调用ReloadData,但无济于事。但是,如果我有所有四个项目,更新工作正常。

代码如下:

CV

我已尝试重新加载public partial class MyView : MvxViewController<MyViewModel> { protected MyCollectionSource CollectionViewSource { get; set; } public override void ViewDidLoad() { base.ViewDidLoad(); this.CollectionViewSource = new MyCollectionSource(this.CollectionView); var set = this.CreateBindingSet<MyView, MyViewModel>(); set.Bind(this.CollectionViewSource).To(vm => vm.Items); set.Bind(this.CollectionViewSource).For(s => s.SelectionChangedCommand).To(vm => vm.ShowDetailCommand); set.Apply(); CollectionViewSource.RemoveCommand = ViewModel.RemoveCommand; this.CollectionView.Source = CollectionViewSource; this.CollectionView.AllowsSelection = true; this.CollectionView.ApplySelectionWorkAround(); this.CollectionView.ReloadData(); } } public class MyCollectionSource : MvxCollectionViewSource { public IMvxCommand<Item> RemoveCommand { get; set; } public MyCollectionSource(UICollectionView collectionView) : base(collectionView) { collectionView.RegisterNibForCell(ItemCell.Nib, ItemCell.Key); collectionView.RegisterNibForCell(AddItemCell.Nib, AddItemCell.Key); collectionView.CollectionViewLayout = new ItemViewLayout(); } protected override UICollectionViewCell GetOrCreateCellFor(UICollectionView collectionView, NSIndexPath indexPath, object item) { return (UICollectionViewCell)collectionView.DequeueReusableCell(GetCellKey(indexPath), indexPath); } protected override object GetItemAt(NSIndexPath indexPath) { if (indexPath.Item == ItemsSource.Count()) return null; var item = base.GetItemAt(indexPath); if (item is Item realItem) { realItem.RemoveCommand = (IMvxCommand)this.RemoveCommand; realItem.Index = indexPath.Row + 1; } return item; } private NSString GetCellKey(NSIndexPath indexPath) { if (indexPath.Item == ItemsSource.Count()) return AddItemCell.Key; return ItemCell.Key; } public override nint GetItemsCount(UICollectionView collectionView, nint section) { return ItemsSource.Count() < MaximumItems ? ItemsSource.Count() + 1 : ItemsSource.Count(); } } public class ItemViewLayout : UICollectionViewFlowLayout { public override CGSize ItemSize { get { return new CGSize(((UIScreen.MainScreen.Bounds.Width - 2 * 8f) - 64f) / 2, 88f); } } } ViewWillAppear上的项目(如果属性名称是用于保存项目的OnViewModelPropertyChanged),尝试将RaisePropertyChanged调用放在导航返回的任何位置到MyView发生,无济于事。但是再次 - 如果ObservableCollection是最大值,它可以正常工作,但如果它更少,则不会。

我正在使用MvvmCross 5.1.1,使用新的导航模式,将所选项目作为参数传递给详细视图。

如何强制CollectionView显示最新数据?

0 个答案:

没有答案