我有一个UICollectionView,我需要独立地听滚动和选择事件。我按照以下方式分配Delegate和Scrolled事件处理程序:
public override void ViewWillAppear(bool animated)
(
base.ViewWillAppear(animated);
this.CollectionView.Delegate = this.CollectionViewDelegate;
this.CollectionView.Scrolled += HandleCollectionViewScrolled;
}但是,在分配事件处理程序之后,将不再调用委托方法。并扭转这种局面:
public override void ViewWillAppear(bool animated)
(
base.ViewWillAppear(animated);
this.CollectionView.Scrolled += HandleCollectionViewScrolled;
this.CollectionView.Delegate = this.CollectionViewDelegate;
}产生完全相反的结果(委托方法可以工作,但没有滚动侦听器)。
考虑到强类型委托对所有方法的必要实现可能会删除事件处理程序,我尝试分配WeakDelegate属性,它是一个只实现collectionView:didSelectItemAtIndexPath:的NSObject子类。
public class MyCollectionViewDelegate : NSObject
{
public MyCollectionViewDelegate() : base()
{
}
[Export ("collectionView:didSelectItemAtIndexPath:")]
public void ItemSelected(UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
{
Console.WriteLine("It worked.");
}
}但是,我还是得到了相同的结果:只有事件处理程序或委托触发。还有其他人经历过吗?这是Xamarin的问题吗?我认为设置弱委托不一定会消除事件处理程序。
值得注意的是,作为一种解决办法,我尝试使用KVO。但是,当我试图观察集合视图的contentOffset属性(可能我使用了错误的键路径名称)时,KVO会使应用程序崩溃。
发布于 2014-02-07 00:01:28
简短回答:
这是故意的。.NET事件是通过使用内部*Delegate实现来实现的(没有其他方法提供它们)。
因此,您不能在不禁用任何现有事件的情况下设置自己的*Delegate。
长答案:
这是我的blog post,描述了这一点。
https://stackoverflow.com/questions/21613789
复制相似问题