tom_gent
tom_gent
CC#
Created by tom_gent on 2/23/2023 in #help
❔ MAUI custom content view only loads content correctly once
the content is mapped like this
public static void MapContent(CarouselScrollViewHandler handler, CarouselScrollView carouselScrollView)
{
_ = handler.PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = handler.VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = handler.MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

if (handler._mauiCarouselScrollView != null)
{
handler._mauiCarouselScrollView.RemoveAllViews();

if (carouselScrollView.Content != null)
{
View virtualContentView = (View)carouselScrollView.PresentedContent;
var contentView = virtualContentView.ToPlatform(handler.MauiContext);
handler._mauiCarouselScrollView.AddView(contentView);

contentView.Measure((int)virtualContentView.WidthRequest, (int)virtualContentView.HeightRequest);

contentView.RequestLayout();
contentView.Invalidate();

handler._mauiCarouselScrollView.RequestLayout();
handler._mauiCarouselScrollView.Invalidate();
}
}
}
public static void MapContent(CarouselScrollViewHandler handler, CarouselScrollView carouselScrollView)
{
_ = handler.PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = handler.VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = handler.MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

if (handler._mauiCarouselScrollView != null)
{
handler._mauiCarouselScrollView.RemoveAllViews();

if (carouselScrollView.Content != null)
{
View virtualContentView = (View)carouselScrollView.PresentedContent;
var contentView = virtualContentView.ToPlatform(handler.MauiContext);
handler._mauiCarouselScrollView.AddView(contentView);

contentView.Measure((int)virtualContentView.WidthRequest, (int)virtualContentView.HeightRequest);

contentView.RequestLayout();
contentView.Invalidate();

handler._mauiCarouselScrollView.RequestLayout();
handler._mauiCarouselScrollView.Invalidate();
}
}
}
3 replies