xamarin-forms-carouselview
xamarin-forms-carouselview copied to clipboard
Cannot access disposed object
Chris, i discovered and issue with your implementation and a way to fix it. Sometimes the Renderer is collected when you close the screen, then, when you open it again and you try to swipe, it crash because Element is null.
The fix?
Android renderer:
void ElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "Renderer") {
_scrollView = (HorizontalScrollView)typeof(ScrollViewRenderer)
.GetField ("hScrollView", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue (this);
_scrollView.HorizontalScrollBarEnabled = false;
_scrollView.Touch += HScrollViewTouch;
}
if (Element == null) return; // this fix the issue
if (e.PropertyName == CarouselLayout.SelectedIndexProperty.PropertyName && !_motionDown) {
ScrollToIndex (((CarouselLayout)this.Element).SelectedIndex);
}
}
iOS renderer:
void ElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
if (Element == null) return; // this fix the issue
if (e.PropertyName == CarouselLayout.SelectedIndexProperty.PropertyName && !Dragging) {
ScrollToSelection (false);
}
}
Thanks for sharing this.
@alexrainman Ran into the issue as well and verified your changes fix it, thanks!
@DennisWelu the only cons this carousel has that it loads all the pages on startup and that is really slow in Android. I was working on one implementation myself https://github.com/alexrainman/CarouselView but Xamarin came with their own on version 2.2-pre-release and i have been working close with them helping to make it work as expected. It's close the be released.
I'm glad you said something! I was just contemplating the way in which I was going to add virtualization to it... I'll check out the pre-release instead. Thanks again!
Yeah, the version that Xamarin is working on might be better suited -- especially concerning virtualization. My intention was to handle those concerns over time, but just never did. Thanks for the feedback :)
https://forums.xamarin.com/discussion/63983/xamarin-forms-2-2-0-pre4-released#latest