5

我设法从代码中设置行和列,但无法将此设置移动到 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>

从我设法只获取 c# 实现的文档中

4

2 回答 2

7

对于具有单个单元格(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>
于 2014-06-21T18:52:20.503 回答
0

这是一个示例

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Label Text="Top Left" Grid.Row="0" Grid.Column="0" />
  <Label Text="Top Right" Grid.Row="0" Grid.Column="1" />
  <Label Text="Bottom Left" Grid.Row="1" Grid.Column="0" />
  <Label Text="Bottom Right" Grid.Row="1" Grid.Column="1" />
</Grid>
于 2017-09-28T12:51:52.210 回答