NavigationPage + CarouselPage:奇怪的滚动错误

时间:2018-12-18 14:04:09

标签: xamarin xamarin.forms carousel

我现在正在使用CarouselPage示例,并且由于我想移至其他页面,因此我以NavigationPage为主要页面来启动应用程序:

    public App()
    {
        InitializeComponent();

        // Need navigation to have modal/nonmodal pages
        //MainPage = new MainPage();
        MainPage = new NavigationPage(new MainPage());
    }

XAML为iPhone X添加了安全区域,并删除了导航栏:

<?xml version="1.0" encoding="utf-8"?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Stoa" x:Class="Test.MainPage"
              xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
              ios:Page.UseSafeArea="true" 
              NavigationPage.HasNavigationBar="False">            
    <ContentPage>
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS, Android" Value="0,40,0,0" />
            </OnPlatform>
        </ContentPage.Padding>
        <StackLayout>
            <Label Text="Green" FontSize="Medium" HorizontalOptions="Center" />
            <Button BackgroundColor="Green" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage>
    <ContentPage>
        <ContentPage.Padding>
            <OnPlatform x:TypeArguments="Thickness">
                <On Platform="iOS, Android" Value="0,40,0,0" />
            </OnPlatform>
        </ContentPage.Padding>
        <StackLayout>
            <Label Text="Blue" FontSize="Medium" HorizontalOptions="Center" />
            <Button BackgroundColor="Blue" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage>
</CarouselPage>

问题在于,现在用户可以垂直滚动,并且显然失去了页边距:

weirdscroll

是否可以解决此问题?

或者,我该如何离开轮播视图或“伪造”轮播?

2 个答案:

答案 0 :(得分:0)

https://github.com/alexrainman/CarouselView上有一篇很棒的文章,说明仓库的所有者创建了自己的轮播,并使用BindableProperties对其进行了扩展,您可以在其中简单地设置方向。

答案 1 :(得分:0)

This "Safe Area scroll offset" can be fixed with a custom iOS CarouselPageRenderer that sets the UIScrollView ContentInsetAdjustmentBehavior to Never

 [assembly: ExportRenderer(typeof(CarouselPage), typeof(CustomCarouselPageRenderer))]
    namespace MyApp.iOS.Renderers
    {
        public class CustomCarouselPageRenderer : CarouselPageRenderer
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
                if (NativeView is UIView view && view.Subviews[0] is UIScrollView scrollView)
                {
                    scrollView.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never;
                }
            }
        }
    }