简体   繁体   中英

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. 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. 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. You should be able to scroll to it.

You could remove the ScrollView around the StackLayout with the image.

[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 :

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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