简体   繁体   English

如何使图像适合堆栈布局或网格?

[英]How to fit an image in a stacklayout or grid?

I have an image that in xamarin form the image adjust to fit in stacklayout.我有一个图像,在 xamarin 中形成图像调整以适合堆栈布局。 Now that i migrated to .net MAUI the image does not adjust but part of the image is hidden by the stacklayout or the stacklayout swallows part of the image.现在我迁移到 .net MAUI,图像没有调整,但部分图像被 stacklayout 隐藏或 stacklayout 吞没了部分图像。 The purple background is another stacklayout that will contain other images that will also fit to the purple stacklayout.紫色背景是另一个堆栈布局,它将包含也适合紫色堆栈布局的其他图像。 在此处输入图像描述

    <StackLayout x:Name="PlayingWindow">
        <StackLayout HorizontalOptions="Center"  VerticalOptions="End"  Padding="2" x:Name="PlayingWindow1" >
            </Grid>

        <Grid Grid.Row="1" >
            <ScrollView HorizontalOptions="Center"  VerticalOptions="End"  Padding="2" x:Name="PlayingWindow1"  >
                <Grid x:Name="mainGrid"    
                  Grid.Row="1"  Padding="2,0" RowSpacing="2" ColumnSpacing="2" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                    <!-- Initialized for Portrait m\ -->
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="1*" />
                    </Grid.ColumnDefinitions>
                    <!-- History display. -->



                    <Label Text="Levels:" Grid.Row="0" Grid.Column="0" BackgroundColor="LightGreen" HeightRequest="15"/>
                    <Label Text="1" Grid.Row="0" Grid.Column="1" x:Name="Level" BackgroundColor="LightGreen" />
                    <Label Text="Timer:" Grid.Row="0" Grid.Column="2"  x:Name="Time" BackgroundColor="DarkGray"  />
                    <Label Text="" Grid.Row="1" Grid.Column="0"  x:Name="Callback" BackgroundColor="DarkGray"  />
                    <Label Text="Commands:" Grid.Row="1" Grid.Column="1" x:Name="Card" BackgroundColor="DarkGray"  />
                    <Label Text="Player6" Grid.Row="1" Grid.Column="2"  x:Name="Player6" BackgroundColor="DarkGray"  />
                    <Label x:Name="TimeContainer1" Text="" BackgroundColor="White"   Grid.Row="1" Grid.Column="2" />
                    <Image Source="" x:Name="ImageCommands" Grid.Row="1" Grid.Column="2" HorizontalOptions="Center"  VerticalOptions="Center" >
                        <Image.GestureRecognizers >
                            <TapGestureRecognizer Tapped ="Imagejokerb" />
                        </Image.GestureRecognizers >
                    </Image>
                    <Button x:Name="initiator" Text="Me" BackgroundColor="LightGreen"   Grid.Row="1" Grid.Column="3" Pressed="play"  CornerRadius="25"  />
                </Grid>



            </ScrollView >
        </Grid>

        </StackLayout >

        <ScrollView HorizontalOptions="Center" HorizontalScrollBarVisibility="Always" Orientation="Horizontal" >
            <StackLayout  Orientation="Horizontal" x:Name="PlayingWindow2">

                <Image Source="pickme.png" x:Name="pickCard" HorizontalOptions="Center"  VerticalOptions="Center"  >
                    <Image.GestureRecognizers >
                        <TapGestureRecognizer Tapped ="TapGestureRecognizer_pickme_5" />
                    </Image.GestureRecognizers >
                </Image>

                <Image  x:Name="droppedCard" HorizontalOptions="Center"  VerticalOptions="Center"  />

            </StackLayout>

        </ScrollView>
        <ScrollView HorizontalOptions="Center" HorizontalScrollBarVisibility="Always" Orientation="Horizontal" >
            <StackLayout Orientation="Horizontal" x:Name="PlayingWindow3" BackgroundColor="MediumPurple">

                <Image Source="pickme.png" x:Name="VariableImagepickmeb" HorizontalOptions="Center"  VerticalOptions="Center" IsVisible="false" >
                    <Image.GestureRecognizers >
                        <TapGestureRecognizer Tapped ="Imagepickmeb" />
                    </Image.GestureRecognizers >
                </Image>
                <Image Source="pickme.png" x:Name="VariableImagepickmer" HorizontalOptions="Center"  VerticalOptions="Center" IsVisible="false" >
                    <Image.GestureRecognizers >
                        <TapGestureRecognizer Tapped ="Imagepickmer" />
                    </Image.GestureRecognizers >
                </Image>
               
            </StackLayout>
        </ScrollView>
    </StackLayout>

I wish to have the picture adjust, to the stacklayout proportion.我希望将图片调整为堆栈布局比例。 Can some help please?可以帮忙吗?

First of all, you have ScrollViews stacked on top of each other in a StackLayout and your image is in a StackLayout inside of one of the ScrollViews, which means the StackLayout that contains the image will have an overflow that is hiding part of the image.首先,您在 StackLayout 中将 ScrollViews 堆叠在彼此之上,并且您的图像位于其中一个 ScrollViews 内部的 StackLayout 中,这意味着包含图像的 StackLayout 将有一个溢出隐藏图像的一部分。 You should be able to scroll to it.您应该可以滚动到它。

You could remove the ScrollView around the StackLayout with the image.您可以使用图像删除 StackLayout 周围的 ScrollView。

[UPDATED] [更新]

At least you should set the Image.Aspect property to AspectFit or Fill : https://learn.microsoft.com/en-us/do.net/maui/user-interface/controls/image#control-image-scaling :至少您应该将Image.Aspect属性设置为AspectFitFillhttps://learn.microsoft.com/en-us/do.net/maui/user-interface/controls/image#control-image-scaling

<ScrollView HorizontalOptions="Center" HorizontalScrollBarVisibility="Always" Orientation="Horizontal">
    <StackLayout Orientation="Horizontal" x:Name="PlayingWindow2">
        <Image Source="pickme.png" x:Name="pickCard" HorizontalOptions="Center" VerticalOptions="Center"
               Aspect="AspectFit">
            <Image.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_pickme_5"/>
            </Image.GestureRecognizers>
        </Image>
        <Image x:Name="droppedCard" HorizontalOptions="Center" VerticalOptions="Center"/>
    </StackLayout>
</ScrollView>

However, auto-sizing images does not work well with ScrollViews when neither Width nor Height are constrained in any way.但是,当 Width 和 Height 都没有以任何方式受到限制时,自动调整图像大小不适用于 ScrollViews。

Also, if you want to keep adding items to the View, then I recommend using a different type of View.另外,如果你想继续向视图添加项目,那么我建议使用不同类型的视图。 You could, for example, use a HorizontalListView ( Sharpnado.CollectionView package ) or a CollectionView or anything that allows you to display a set of Views or Data, because they are better suited for dynamic layouts and data compared to a StackLayout wrapped inside a ScrollView, which is more appropriate for View elements that are known at compile time.例如,您可以使用 Horizo HorizontalListView ( Sharpnado.CollectionView package ) 或CollectionView或任何允许您显示一组视图或数据的东西,因为与包裹在 ScrollView 中的 StackLayout 相比,它们更适合动态布局和数据,这更适合于在编译时已知的视图元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM