Xamarin Forms:ios:Page.UseSafeArea 不适用于 CarouselPage 中的所有 children

Xamarin Forms: ios:Page.UseSafeArea is not working for all the children in CarouselPage

我有一个有 4 个 children 的 CarouselPage。对于我添加的每个页面 ios:Page.UseSafeArea,但 SafeArea 属性 仅适用于前 2 页,而对于其余 2 页,安全区域功能不起作用。我的 XF 版本是 4.7.0.1080(最新的)。

我在 Github 上发现了同样的问题,这个问题有什么解决方案吗?

创建自定义渲染器以编写您自己的 safeAreaInsets,然后将其分配给 Page.Padding。这里使用200ms的延迟等待UIApplication.SharedApplication.KeyWindow.

的初始化
[assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
namespace App19F_8.iOS
{
    public class CustomPageRenderer : PageRenderer
    {
        public CustomPageRenderer()
        {
        }

        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);


            if (e.NewElement != null)
            {
                var inset = new Thickness();

                Device.StartTimer(new TimeSpan(200), () =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        if (UIApplication.SharedApplication.KeyWindow != null)
                        {
                            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                            {
                                var safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
                                inset.Top = safeAreaInsets.Top;
                                inset.Bottom = safeAreaInsets.Bottom;
                                inset.Left = safeAreaInsets.Left;
                                inset.Right = safeAreaInsets.Right;
                            }
                        }

                        ContentPage page = e.NewElement as ContentPage;
                        page.Padding = inset;
                    });
                    return false;
                });
            }
        }
    }
}

在您的 Xamarin.forms 项目中,使 CarouselPage 中的页面继承自 CustomPage