diff --git a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs index d0a4b0af94b9..102806094e8d 100644 --- a/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs +++ b/src/Controls/src/Core/Handlers/Items/Android/MauiCarouselRecyclerView.cs @@ -19,7 +19,7 @@ public class MauiCarouselRecyclerView : MauiRecyclerView _oldViews; - CarouselViewwOnGlobalLayoutListener _carouselViewLayoutListener; + CarouselViewOnGlobalLayoutListener _carouselViewLayoutListener; protected CarouselView Carousel => ItemsView as CarouselView; @@ -516,13 +516,11 @@ void AddLayoutListener() if (_carouselViewLayoutListener != null) return; - _carouselViewLayoutListener = new CarouselViewwOnGlobalLayoutListener(); - _carouselViewLayoutListener.LayoutReady += LayoutReady; - + _carouselViewLayoutListener = new CarouselViewOnGlobalLayoutListener(this); ViewTreeObserver.AddOnGlobalLayoutListener(_carouselViewLayoutListener); } - void LayoutReady(object sender, EventArgs e) + void LayoutReady() { if (!_initialized) { @@ -546,7 +544,6 @@ void ClearLayoutListener() return; ViewTreeObserver?.RemoveOnGlobalLayoutListener(_carouselViewLayoutListener); - _carouselViewLayoutListener.LayoutReady -= LayoutReady; _carouselViewLayoutListener = null; } @@ -594,14 +591,23 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) base.OnMeasure(widthMeasureSpec, heightMeasureSpec); } - } - class CarouselViewwOnGlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener - { - public EventHandler LayoutReady; - public void OnGlobalLayout() + class CarouselViewOnGlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener { - LayoutReady?.Invoke(this, new EventArgs()); + readonly WeakReference _recyclerView; + + public CarouselViewOnGlobalLayoutListener(MauiCarouselRecyclerView recyclerView) + { + _recyclerView = new(recyclerView); + } + + public void OnGlobalLayout() + { + if (_recyclerView.TryGetTarget(out var recyclerView)) + { + recyclerView.LayoutReady(); + } + } } } } diff --git a/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs b/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs index ee28a02795a7..d537614ce7e4 100644 --- a/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/NavigationPage/NavigationPageTests.cs @@ -37,6 +37,7 @@ void SetupBuilder() handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); + handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(typeof(Controls.ContentView), typeof(ContentViewHandler)); handlers.AddHandler(typeof(ScrollView), typeof(ScrollViewHandler)); @@ -315,11 +316,12 @@ await CreateHandlerAndAddToWindow(new Window(navPage), async Title = "Page 2", Content = new VerticalStackLayout { - new Label(), new Button(), + new CarouselView(), new CollectionView(), + new ContentView(), + new Label(), new ScrollView(), - new ContentView() } }; pageReference = new WeakReference(page);