【发布时间】:2017-01-30 19:01:21
【问题描述】:
Xamarin Forms 尝试确保图像与其包含列的宽度匹配时遇到问题。 (想想带有图片和描述的简单产品列表)。
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#代码是:
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);
}
}
我已经应用了各种选项,例如将图像包装在堆栈布局中(带有堆栈布局选项)以及将 AspectFit 和 EndAndExpand 添加到图像中,但它永远不会 100% 正确。Resultant view from a 5" KitKat Android Emulator
有什么建议吗?
谢谢
【问题讨论】:
标签: c# xaml xamarin.forms