I have a UICollectionView
from which I need to listen to scrolling and selection events independently. I assign the Delegate
and the 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 I assign the event handler, the delegate methods no longer get called. And reversing them:
public override void ViewWillAppear(bool animated)
(
base.ViewWillAppear(animated);
this.CollectionView.Scrolled += HandleCollectionViewScrolled;
this.CollectionView.Delegate = this.CollectionViewDelegate;
}
yields the exact opposite result (the delegate methods working but no scrolled listener).
Thinking that the strongly typed delegate's necessary implementation of all methods might be wiping the event handlers out, I tried instead to assign the WeakDelegate
property, which is an NSObject
subclass 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 again, I get the same result: only the event handler or the delegate fires. Has anyone else experienced this? Is this an issue with Xamarin? I would expect that setting the weak delegate shouldn't necessarily wipe out the event handlers.
It's also worth noting that as a workaround, I tried using KVO. But KVO crashes the app when I try to observe the contentOffset
property of the collection view (perhaps I'm using the wrong key path name).
EventToCommand Behavior EventToCommandBehavior class is a reusable Xamarin.Forms custom behavior that executes a command in response to any event firing. The following behavior properties must be set to use the EventToCommand behavior: EventName – the name of the event the behavior listens to (Ex: Appearing).
The Xamarin.Forms SwipeView is a container control that wraps around an item of content, and provides context menu items that are revealed by a swipe gesture.
Check the ViewModel here. Control template is a very handy Xamarin Forms functionality that helps you to separate the user interface (UI) of a custom view or page from the specific logic that implements the control or page. If you want to know more about it, I recommend you can check this great article by Xamboy.
The SwipeContainer class creates SwipeGestureRecognizer objects for all four swipe directions, and attaches Swipe event handlers. These event handlers invoke the Swipe event defined by the SwipeContainer. The following XAML code example shows the SwipeContainer class wrapping a BoxView:
Short Answer:
This is by design. The .NET events are implemented by using an internal *Delegate
implementation (there's simply no other way to provide them).
As such you cannot set your own *Delegate
without disabling any existing events.
Long Answer:
Here's my blog post that describe this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With