Xamarin - Assigning a UICollectionViewDelegate erases event handlers (and vice versa)

I have a UICollectionView from which I need to listen to scroll and select events on my own. I assign a Delegate and Scrolled event handler as follows:

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

However, after the event handler is assigned, the delegate methods are no longer called. And turning them:

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

gives the exact opposite result (delegate methods work but no scrollable listener).

Thinking that the strongly typed delegate needed to implement all methods can wipe event handlers, I tried instead to assign the WeakDelegate property, which is a subclass of NSObject that only implements 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."); } } 

But then again, I get the same result: only the event handler or delegate fires. Has anyone else experienced this? Is this a problem with Xamarin? I would expect that setting a weak delegate does not have to destroy event handlers.

It's also worth noting that as a workaround, I tried using KVO. But KVO crashes the application when I try to observe the contentOffset property of the contentOffset view (maybe I'm using the wrong path name).

+6
source share
1 answer

Short answer:

This is by design. .NET events are implemented using an internal *Delegate implementation (there is simply no other way to provide them).

Thus, you cannot set your *Delegate without disabling any existing events.

Long answer:

Here is my blog post that describes this.

+5
source

All Articles