C# 使用Xamarin.Forms创建一个3X2网格,每个单元格都是正方形
我是Xamarin的新手。现在我需要在我的页面中设计一个网格,网格是3X2(2行3列)。但是我需要这个网格中的所有单元格都是方形的,并且这个网格的宽度与它的父视图相匹配 换句话说,假设屏幕(或该网格的父视图)的宽度为3,则每列的宽度为1。如何强制每行的高度正好为1(以便网格的每个单元格的高度等于宽度) 这是我的设计,但不是作品:C# 使用Xamarin.Forms创建一个3X2网格,每个单元格都是正方形,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我是Xamarin的新手。现在我需要在我的页面中设计一个网格,网格是3X2(2行3列)。但是我需要这个网格中的所有单元格都是方形的,并且这个网格的宽度与它的父视图相匹配 换句话说,假设屏幕(或该网格的父视图)的宽度为3,则每列的宽度为1。如何强制每行的高度正好为1(以便网格的每个单元格的高度等于宽度) 这是我的设计,但不是作品: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
这个问题有没有一个纯XAML解决方案
感谢您的帮助。
Star
调整大小根据方向使用可用空间进行调整,因此您需要通过覆盖OnSizeAllocated
方法手动调整大小(调整子对象大小时请小心嵌套方法调用)
Xaml:
<Grid x:Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
<Grid x:Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" x:Name="firstCell" />
<!-- Further declaration of elements inside the grid... -->
</Grid>
我有一个更简单的方法。诀窍是命名第一个单元格,并检索其宽度 首先定义布局:
<Grid x:Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
<Grid x:Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" x:Name="firstCell" />
<!-- Further declaration of elements inside the grid... -->
</Grid>
谢谢然而,我对
如果(3*height>2*height)
有点困惑。让我们用一个例子来解释一下:如果我们假设每个正方形边缘都是1”,那么您的容器应该是3英寸,高度为2英寸“。您的网格
应遵守此3x2刻度。该条件检查我们是否减少了宽度
或高度
@eakgul-大概你的意思是3*宽度-将高度
与高度
进行比较是不明智的-因此艾伦感到困惑。。。。或者可能是第二个高度
应该是宽度
。任何使用此方法的人都应该从两个方面测试不同的情况。。。如果(宽度/3>高度/2),则更清晰的测试将是。我不知道后面的公式是否正确。需要注释来解释算法的代码的好例子。值得一提的是,这在XAML中假设:这对我不起作用。高度比宽度大一点。
<Grid x:Name="mainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0" x:Name="firstCell" />
<!-- Further declaration of elements inside the grid... -->
</Grid>
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
int colCount = mainGrid.ColumnDefinitions.Count;
int rowCount = mainGrid.RowDefinitions.Count;
if (colCount > 0 && rowCount > 0)
{
mainGrid.HeightRequest = (firstCell.Width + mainGrid.RowSpacing) * rowCount - mainGrid.RowSpacing;
}
}