如何设置Xamarin.Forms Xaml中的行/列定义?

问题描述:

我设法从代码设置的行和列,但不能动这个设置为XAML:如何设置Xamarin.Forms 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

您可以定义 “它不工作”?你的xaml看起来是有效的,而且这是正确的做法。 –

+0

它没有给所有的屏幕空间的网格内容与行= 0和col = 0。相反,内容转移到表单的左上角。虽然如果我使用代码定义 - 如果工作,它给所有的空间内部控制是去中心 –

+0

这件事情应该工作。你可以发布更完整的例子,代码和你正在做的xaml。谢谢 –

我也得到了与单个单元格(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> 
    <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>