c# - Xamarin.Forms Scrollview 不会滚动

标签 c# xaml xamarin xamarin.forms

我正在创建一个在 ScrollView 中包含 Stacklayout 的应用程序。在两者中,我都将 Orientation 定义为 Vertical,将 VerticalOptions 定义为 FillAndExpand。如此处所述:https://forums.xamarin.com/discussion/20945/scrollview-in-xaml 但是应用程序不会滚动。

<ContentPage.Content>
    <AbsoluteLayout>
        <ScrollView Orientation="Vertical"
                    VerticalOptions="FillAndExpand">
            <StackLayout Margin="{StaticResource PageMargin}"
                         AbsoluteLayout.LayoutBounds="0.0, 0.0, 1.0, 1.0"
                         AbsoluteLayout.LayoutFlags="All"
                         Orientation="Vertical"
                         VerticalOptions="FillAndExpand">

                <Label Text="Lorem Ipsum..." />
                <!--Extra Labels-->
                <Label Text="Lorem Ipsum..." />

            </StackLayout>
        </ScrollView>

        <skia:SKCanvasView x:Name="BgCanvasView"
                           PaintSurface="OnBgCanvasViewPaintSurface"
                           AbsoluteLayout.LayoutBounds="0.0, 0.0, 1.0, 1.0"
                           AbsoluteLayout.LayoutFlags="All"
                           InputTransparent="True"/>
    </AbsoluteLayout>
</ContentPage.Content>

你有什么我可以尝试的想法吗?

提前谢谢你。

编辑:我发现它与 <AbsoluteLayout> 有关.我创建了另一个项目来测试这个问题。使用 AbsoluteLayout 与我的应用程序中的结果相同。但如果没有它,StackLayout 会按预期滚动。由于 AbsoluteLayout 对我来说是必需的,因为我在 SKCanvasView 中绘制的背景图形,错过它不是一个选项。

编辑:好吧......我自己弄明白了,现在觉得有点傻。 <Scrollview>缺少 AbsoluteLayout.LayoutBounds 和 AbsoluteLayout.LayoutFlags

最佳答案

  • 你的方向是正确的,只是不要忘记,如果我们想要 ScrollView 滚动,那么 ScrollView 的内容必须溢出 ScrollView 边界
  • 目前您的 ScrollView 和 StackLayout 都在 AbsoluteLayout 中,AbsoluteLayout.LayoutBounds = "All"这意味着高度和宽度将与屏幕或父 View 成比例。
  • 由于这个 StackLayout 是您的 ScrollView 的子元素,它永远不会大于 ScrollView 的边界,并且 ScrollView 永远不会允许您滚动屏幕。

请尝试按照以下方法实现 Scrollview,它可能会有所帮助

<AbsoluteLayout>
    <StackLayout AbsoluteLayout.LayoutBounds = "0,0,1,1" AbsoluteLayout.LayoutFlags = "All">
       <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
          <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"></StackLayout>
       </ScrollView>
    </StackLayout>
    <skia:SKCanvasView x:Name="BgCanvasView"
                       PaintSurface="OnBgCanvasViewPaintSurface"
                       AbsoluteLayout.LayoutBounds="0.0, 0.0, 1.0, 1.0"
                       AbsoluteLayout.LayoutFlags="All"
                       InputTransparent="True"/>
</AbsoluteLayout>

我在我的机器上尝试了相同的代码,scrollview 对我来说工作正常

关于c# - Xamarin.Forms Scrollview 不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55372993/

相关文章:

xamarin - Xamarin iOS应用程序被App Store拒绝,因为它不能在仅IPv6的环境中使用

c# - 根据输入框获取不同的url

C# - System.Windows.Forms.Clipboard.GetDataObject() 没有响应

WPF动态布局: how to enforce square proportions (width equals height)?

Wpf 样式 : Binding to Child Property via ElementName

xaml - 3 Pane 循环滚动面板 - 建议

javascript - C# Javascript 日期/时间不匹配

c# - Discord C# 用户加入消息

android - 如何在普通项目中使用 SQLite 和 Xamarin

xamarin - 如何在 iOS 16 中更改方向?