Xamarin.Forms 中的水平滚动视图,具有类似 AspectFill 的图像缩放功能

问题描述 投票:0回答:1

我目前正在开发一个 Xamarin.Forms 项目,并尝试创建带有图像的水平滚动视图。我希望图像的缩放比例类似于 Windows 窗体中的 PictureBoxSizeMode.Zoom 行为。我尝试了几种方法,包括在 ScrollView 中使用 StackLayout 以及将 Image 元素上的 Aspect 属性设置为 AspectFill。

但是,无论我采取哪种方法,布局总是需要水平滚动。我想确保图像适合可用空间而不需要滚动。

这是我的 XAML 的简化版本:

<ScrollView HorizontalScrollBarVisibility="Always">
    <StackLayout x:Name="menuStack1" Orientation="Horizontal" WidthRequest="500">
        <Image Source="your_image.png" Aspect="AspectFill" HeightRequest="50" WidthRequest="50" />
        <Image Source="your_image.png" Aspect="AspectFill" HeightRequest="50" WidthRequest="50" />
        <!-- Add more Image elements as needed -->
    </StackLayout>
</ScrollView>

我尝试了不同的 WidthRequest 值和图像尺寸,但问题仍然存在。我缺少什么?如何实现图像在可用空间内动态缩放而不需要水平滚动的所需行为?

任何帮助或指导将不胜感激!预先感谢。

c# xamarin xamarin.forms
1个回答
1
投票
    <StackLayout>
        <!-- First ScrollView with Button -->
        <StackLayout>
            <ScrollView Orientation="Horizontal">
                <StackLayout Orientation="Horizontal">
                    <Button Text="File" BackgroundColor="Transparent" TextColor="White" Clicked="Button_Clicked" TextTransform="None"/>
                </StackLayout>
            </ScrollView>
            <ScrollView Orientation="Horizontal">
                <StackLayout Orientation="Horizontal">
                    <ImageButton Source="C:/Users/user/Desktop/icon.png" HeightRequest="60" WidthRequest="60" Aspect="AspectFit" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"/>
                </StackLayout>
            </ScrollView>
            <!-- Add more elements as needed -->
        </StackLayout>
    </StackLayout>
© www.soinside.com 2019 - 2024. All rights reserved.