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

时间:2014-02-06 20:51:22

标签: 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;
}

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

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

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

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

collectionView:didSelectItemAtIndexPath:

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

值得注意的是,作为一种解决方法,我尝试使用KVO。但是当我尝试观察集合视图的public class MyCollectionViewDelegate : NSObject { public MyCollectionViewDelegate() : base() { } [Export ("collectionView:didSelectItemAtIndexPath:")] public void ItemSelected(UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath) { Console.WriteLine("It worked."); } } 属性时,KVO会崩溃应用程序(可能我使用了错误的密钥路径名称)。

1 个答案:

答案 0 :(得分:5)

简答:

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

因此,在不禁用任何现有事件的情况下,您无法设置自己的*Delegate

长答案:

这是我的blog post描述这一点。

相关问题