C# 表单图像导致网格行高度过度增加
网格已经用foreach循环外部代码开头的列实例化,在这个循环中,行正在实例化C# 表单图像导致网格行高度过度增加,c#,xamarin.forms,grid,row,row-height,C#,Xamarin.forms,Grid,Row,Row Height,网格已经用foreach循环外部代码开头的列实例化,在这个循环中,行正在实例化 foreach (var post in posts) { Frame featuredFrame = new Frame(); featuredFrame.Padding = 20; featuredFrame.Margin = new Thickness(0, 10, 0, 0); Label postTitle = new Label(); postTitle.Font
foreach (var post in posts)
{
Frame featuredFrame = new Frame();
featuredFrame.Padding = 20;
featuredFrame.Margin = new Thickness(0, 10, 0, 0);
Label postTitle = new Label();
postTitle.FontSize = Device.GetNamedSize(NamedSize.Subtitle, typeof(Label));
BoxView titleSeparator = new BoxView();
titleSeparator.Color = Color.Gray;
titleSeparator.HeightRequest = 1;
titleSeparator.HorizontalOptions = LayoutOptions.Fill;
Image postFeaturedImage = new Image();
postFeaturedImage.Source = ImageSource.FromUri(imageUri);
postFeaturedImage.Aspect = Aspect.AspectFill;
BoxView imageSeparator = new BoxView();
imageSeparator.Color = Color.Gray;
imageSeparator.HeightRequest = 2;
imageSeparator.HorizontalOptions = LayoutOptions.Fill;
Label publishDate = new Label();
publishDate.FontSize = Device.GetNamedSize(NamedSize.Caption, typeof(Label));
StackLayout postDetails = new StackLayout();
postDetails.Children.Add(postTitle);
postDetails.Children.Add(titleSeparator);
postDetails.Children.Add(postFeaturedImage);
postDetails.Children.Add(imageSeparator);
postDetails.Children.Add(publishDate);
postDetails.Margin = new Thickness(0);
postDetails.Padding = new Thickness(0);
featuredFrame.Content = postDetails;
postGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
postGrid.Children.Add(featuredFrame, columnNumber, rowNumber);
}
如您所见,创建行的高度设置为“自动”,这意味着行与子行的高度相同
这是上述代码的结果:
这是上面代码与postdails.Children.Add(postFeaturedImage)的结果代码>已注释掉:
在带有图像的屏幕截图中,发布日期时间文本下方有大量空白。在没有图像的截图中,这个空白区域消失了
我需要图片可见,但空白空间不应该在那里。如何解决这个问题?我遇到了同样的问题(这就是我如何找到问题的原因),我已经设法解决了
我通过添加垂直选项解决了这个问题,如下所示:
postdails.VerticalOptions=LayoutOptions.Start代码>
并确保您有Height=GridLength.Auto
用于RowDefinition
我遇到了相同的问题(这就是我发现问题的原因),我已设法解决了它
我通过添加垂直选项解决了这个问题,如下所示:
postdails.VerticalOptions=LayoutOptions.Start代码>
并确保您的Height=GridLength.Auto
用于RowDefinition
这个问题是在IOS中发生的吗?您可以尝试为您的图像或您的帧设置特定的HeightRequest
,我在Android中使用不同的图片高度对其进行测试,结果如下屏幕截图所示:。它似乎正常工作。不,我也在Android上测试过,问题也存在。设置特定的HeightRequest
会导致网格出现故障,例如,它重叠并且不适合ScrollView
只要尝试使用CollectionView,这可能比尝试自己构建所有网格更正确。这个问题是不是在IOS中发生的?您可以尝试为您的图像或您的帧设置特定的HeightRequest
,我在Android中使用不同的图片高度对其进行测试,结果如下屏幕截图所示:。它似乎正常工作。不,我也在Android上测试过,问题也存在。设置特定的HeightRequest
会导致网格出现故障,例如,它重叠并且不适合ScrollView
只要尝试使用CollectionView,这可能比尝试自己构建所有网格更正确。