我设法从代码中设置行和列,但无法将此设置移至 xaml:
grid.RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
};
grid.ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
};
以下方法不起作用:
<Grid x:Name="grid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
...
</Grid>
来自documentation我设法只获取 C# 实现
最佳答案
对于具有单个单元格的网格(1行/列,尽管我不确定为什么我们需要一个具有全尺寸屏幕的单个单元格的网格),我也得到相同的行为(不填充和展开) ,但它似乎适用于 2 x 2、3 x 3 单元格网格(尚未尝试其他)。
Xamarin Forms 中需要 Height=""和 Width=""属性,尽管我“认为”WPF 中不需要它们,因为默认情况下是这样的。
<ContentPage.Content>
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Text="1"></Button>
<Button Grid.Row="1" Grid.Column="1" Text="2"></Button>
</Grid>
</ContentPage.Content>
关于grid - 如何在 Xamarin.Forms Xaml 中设置行/列定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24315023/