Xamarin表單試圖確保圖像與其包含列的寬度匹配時出現問題。 (想象一下帶有圖片和說明的簡單產品列表)。Xamarin表單 - 獲取圖像以填充包含網格列的寬度?
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
有什麼建議嗎?
感謝
感謝您的支持。我去了第二個確實解決問題的選項。但是我注意到了其他一些東西: 如果原始圖像對於空間來說太大,那麼行高會擴展到原始大小(寬度很好),儘管實際生成的圖像在頂部很好地放置正確。底部有這麼大的空間嗎? –