【问题标题】:How to setup Row/Column Definitions in Xamarin.Forms Xaml?如何在 Xamarin.Forms Xaml 中设置行/列定义?
【发布时间】:2014-08-10 11:43:45
【问题描述】:

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

【问题讨论】:

  • 你能定义“它不起作用”吗?您的 xaml 看起来有效,这是正确的做法。
  • 它不会为 row=0 和 col=0 的网格内容提供所有屏幕空间。相反,内容被转移到表单的左上角。虽然如果我使用代码中的定义 - 如果有效,它会为内部控件提供所有空间
  • 这个东西应该可以工作。您能否发布更完整的示例、代码和您正在做的事情的 xaml。谢谢

标签: grid xamarin xamarin.forms


【解决方案1】:

对于具有单个单元格(1 行/列,尽管我不确定为什么我们需要具有单个单元格全屏尺寸的网格)的网格,我也得到相同的行为(不填充和扩展) ,但它似乎适用于 2 x 2、3 x 3 单元格网格(还没有尝试过其他网格)。

Height="" 和 Width="" 属性在 Xamarin 表单中是必需的,尽管我“认为”它们在 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>

【讨论】:

    【解决方案2】:

    这是一个示例

    <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>
    

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 1970-01-01
      • 2012-09-09
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2014-11-10
      相关资源
      最近更新 更多