如果在下方添加内容,则不接受 HeightRequest

HeightRequest is not honored if content is added below

我正在尝试锁定页眉的高度 - 或者基本上是页面顶部的任何类型的内容。但是,一旦将可滚动内容添加到下方,高度就会发生变化。一个简单的例子:

<ContentPage.Content>
        <StackLayout Padding="0" Margin="0" Spacing="0">
            <Frame Margin="0" HeightRequest="150" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
        </StackLayout>
</ContentPage.Content>

这很好用。但是,在滚动视图中添加大量简单的标签行会以某种方式改变框架的高度。

<ContentPage.Content>
        <StackLayout Padding="0" Margin="0" Spacing="0">
            <Frame Margin="0" HeightRequest="150" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
            <ScrollView>
                <StackLayout>
                    <Label Text="aaa" /> <!-- Repeated 54 times however removed here for simplicity-->
                </StackLayout>
            </ScrollView>
        </StackLayout>
</ContentPage.Content>

结果是这样的:

为什么会发生这种情况,我可以采取任何措施来避免这种情况吗?为什么高度会降低到恰好那个高度?谢谢!

您似乎没有限制 ScrollView 的高度。因此,考虑到您只是在执行 HeightRequest,它可能会根据需要进行更改以适合所有内容。 使用网格通常更安全。在这个例子中,类似这样的东西可以帮助你:

<ContentPage.Content>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Frame Grid.Row="0" Margin="0" BackgroundColor="red" CornerRadius="0" HasShadow="False" Padding="0"></Frame>
        <ScrollView Grid.Row="1">
            <StackLayout>
                <Label Text="aaa" /> <!-- Repeated 54 times however removed here for simplicity-->
            </StackLayout>
        </ScrollView>
    </Grid>
</ContentPage.Content>