Xamarin - 分配UICollectionViewDelegate消除了事件处理程序(反之亦然)

enj*_*yem 6 c# xamarin.ios xamarin uicollectionview

我有一个UICollectionView我需要独立收听滚动和选择事件的地方.我按如下方式分配DelegateScrolled事件处理程序:

public override void ViewWillAppear(bool animated)
(
    base.ViewWillAppear(animated);
    this.CollectionView.Delegate = this.CollectionViewDelegate;
    this.CollectionView.Scrolled += HandleCollectionViewScrolled;
}
Run Code Online (Sandbox Code Playgroud)

但是,在我分配事件处理程序之后,不再调用委托方法.并扭转他们:

public override void ViewWillAppear(bool animated)
(
    base.ViewWillAppear(animated);
    this.CollectionView.Scrolled += HandleCollectionViewScrolled;
    this.CollectionView.Delegate = this.CollectionViewDelegate;
}
Run Code Online (Sandbox Code Playgroud)

产生完全相反的结果(委托方法工作但没有滚动的监听器).

认为强类型委托的所有方法的必要实现可能会消除事件处理程序,我尝试分配WeakDelegate属性,这是一个NSObject仅实现的子类collectionView:didSelectItemAtIndexPath:.

public class MyCollectionViewDelegate : NSObject
{
    public MyCollectionViewDelegate() : base()
    {
    }

    [Export ("collectionView:didSelectItemAtIndexPath:")]
    public void ItemSelected(UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        Console.WriteLine("It worked.");
    }
}
Run Code Online (Sandbox Code Playgroud)

但同样,我得到了相同的结果:只有事件处理程序或委托触发.还有其他人经历过这个吗?这是Xamarin的问题吗?我期望设置弱委托不应必然消灭事件处理程序.

值得注意的是,作为一种解决方法,我尝试使用KVO.但是当我尝试观察contentOffset集合视图的属性时,KVO崩溃了应用程序(也许我使用了错误的密钥路径名称).

pou*_*pou 5

简答:

这是设计的..NET事件是通过使用内部 *Delegate实现来实现的(没有其他方法可以提供它们).

因此,*Delegate如果不禁用任何现有事件,则无法设置自己的事件.

答案很长:

这是我的博客文章,描述了这一点.