我们有一个使用自定义 MvxCollectionViewSource 作为源的 UICollectionView。选择项目时,我们想滚动到项目,做一个简短的动画,然后做一个操作。在源中,ItemSelected 被覆盖如下,其中为 ScrollAnimationEnded 设置了一个事件。
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
var item = GetItemAt(indexPath) as SlideMenuViewItemModel;
var cell = collectionView.VisibleCells[0];
var index = collectionView.IndexPathForCell(cell);
if (!Equals(index, indexPath))
{
collectionView.ScrollAnimationEnded += HandleAnimationDone;
collectionView.ScrollToItem(indexPath, UICollectionViewScrollPosition.CenteredHorizontally, true);
}
else
{
var layout = (SlideMenuLayout)collectionView.CollectionViewLayout;
layout.SelectedIndexPath = indexPath;
collectionView.PerformBatchUpdates(delegate { }, delegate
{
item.OnClick.Execute(null);
layout.SelectedIndexPath = null;
});
}
}
private void HandleAnimationDone(object sender, EventArgs args)
{
CollectionView.ScrollAnimationEnded -= HandleAnimationDone;
var layout = (SlideMenuLayout)CollectionView.CollectionViewLayout;
CollectionView.PerformBatchUpdates(delegate { }, delegate
{
// item.OnClick.Execute(null);
layout.SelectedIndexPath = null;
});
}
问题是它只在第一次工作。选择项目并执行滚动并触发事件。但是,当尝试进行下一个选择时,永远不会调用 ItemSelected。如果我删除行 collectionView.ScrollAnimationEnded += HandleAnimationDone 它会继续工作。所以似乎连接事件会破坏一些东西,或者我需要调用一些东西来重置。
我希望你有一个建议:)
问候