简体   繁体   English

Xamarin表单 - 获取图像以填充包含网格列的宽度?

[英]Xamarin Forms - Get Image to Fill width of containing Grid Column?

Having an issue with Xamarin Forms trying to ensure an image matches the width of its containing column. Xamarin Forms遇到问题,试图确保图像与其包含列的宽度匹配。 (Think of a simple product listing with a picture and description). (想象一下带有图片和描述的简单产品清单)。

XAML code is: XAML代码是:

 <?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="formsApp.MainPage"> <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness" iOS="20, 40, 20, 20" Android="20, 20, 20, 20" WinPhone="20, 20, 20, 20" /> </ContentPage.Padding> <ContentPage.Content> <ScrollView> <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="15" > <Grid x:Name="testGrid" VerticalOptions="Center"> </Grid> </StackLayout> </ScrollView> </ContentPage.Content> </ContentPage> 

C# code behind is: C#代码背后是:

public MainPage()
    {
        InitializeComponent();

        testGrid.BackgroundColor = Color.Gray;
        testGrid.Padding = new Thickness(10D);
        testGrid.RowDefinitions = new RowDefinitionCollection();
        testGrid.ColumnDefinitions = new ColumnDefinitionCollection();

        testGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        testGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(3, GridUnitType.Star) });

        for (int MyCount = 0; MyCount < 20; MyCount++)
        {
            testGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
        }

        for (int i = 0; i < testGrid.RowDefinitions.Count(); i++)
        {
            StackLayout st = new StackLayout() { Orientation = StackOrientation.Horizontal, HorizontalOptions = LayoutOptions.Center, IsClippedToBounds = true };

            st.Children.Add(new Image
            {
                Source = ImageSource.FromUri(new Uri("https://lh3.googleusercontent.com/nYhPnY2I-e9rpqnid9u9aAODz4C04OycEGxqHG5vxFnA35OGmLMrrUmhM9eaHKJ7liB-=w300")),
                Aspect = Aspect.AspectFit,
                HorizontalOptions = LayoutOptions.EndAndExpand
            });

            testGrid.Children.Add(st, 0, i);

            testGrid.Children.Add(new Label
                {
                    Text = "Row" + i + " description.... fdsfsdf sdf sdfsd fsdf sdf sdf sdfsd fsd fsdf sdf sd fsd fsdf "
            }, 1, i);

        }

    }

I have applied various options such as wrapping the image in a Stack layout (with options on stack layout) and also adding AspectFit and EndAndExpand to the image but it is never 100% correct. 我已经应用了各种选项,例如将图像包装在堆栈布局中(包含堆栈布局上的选项),还将AspectFit和EndAndExpand添加到图像中,但它永远不会100%正确。 Resultant view from a 5" KitKat Android Emulator 来自5“KitKat Android模拟器的结果视图

Any advice? 有什么建议?

Thanks 谢谢

I think the issue here is the EndAndExpand horizontal option. 我认为这里的问题是EndAndExpand水平选项。 As far as I have understood, the ...AndExpand expand the respective views if the content would not fit otherwise. 据我...AndExpand ,如果内容不合适, ...AndExpand 扩展相应的视图。 Furthermore you've set the column widths of the grid to * and 3* which would mean, that the first column would take a fourth of the grid width, hence there is no space to expand. 此外,您已将网格的列宽设置为*3* ,这意味着第一列将占据网格宽度的四分之一,因此没有可扩展的空间。

There are multiple possible solutions to this issue - depending on how you want the result to look. 这个问题有多种可能的解决方案 - 取决于您希望结果的外观。 You could go for Auto and * regarding the columns 您可以选择Auto*关于列

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

or you could make the image use only the available space 或者您可以使图像仅使用可用空间

st.Children.Add(new Image
{
    Source = "...",
    Aspect = Aspect.AspectFit,
    HorizontalOptions = LayoutOptions.Fill
});

Both options should solve your problem. 这两个选项都可以解决您的问题

But , please note, that you could even do better. 但是 ,请注意,你甚至可以做得更好。 Instead of using a grid, you could use a ListView . 您可以使用ListView而不是使用网格。 With ListView s you can use the power of data binding for your cells. 使用ListView您可以为您的单元格使用数据绑定的强大功能。 You would not have to clutter your code with all that view-building code, but could declare everything in neat XAML. 您不必使用所有构建视图的代码来混淆代码,但可以在整洁的XAML中声明所有内容。

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

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