xamarin-forms-carouselview icon indicating copy to clipboard operation
xamarin-forms-carouselview copied to clipboard

Cannot access disposed object

Open alexrainman opened this issue 9 years ago • 5 comments
trafficstars

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 avatar Jan 13 '16 17:01 alexrainman

@alexrainman Ran into the issue as well and verified your changes fix it, thanks!

DennisWelu avatar Apr 20 '16 04:04 DennisWelu

@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.

alexrainman avatar Apr 20 '16 05:04 alexrainman

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!

DennisWelu avatar Apr 20 '16 12:04 DennisWelu

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 :)

chrisriesgo avatar Apr 25 '16 21:04 chrisriesgo

https://forums.xamarin.com/discussion/63983/xamarin-forms-2-2-0-pre4-released#latest

chrisriesgo avatar Apr 25 '16 21:04 chrisriesgo