XAML 如何在.Net Maui中创建第一个单元格为正方形的网格?

jc3wubiy  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(122)

我想创建一个网格,它的第一个单元格是正方形的,即它的高度等于它的宽度,这是由屏幕宽度决定的。我想像下面的东西,但它没有工作。

<Grid RowDefinitions ="{DeviceDisplay.Current.MainDisplayInfo.Width}, *">
... ...
</Grid>
1cklez4t

1cklez4t1#

是的,您可以在代码中实现这一点。
我创建了一个demo,可以参考下面的代码:

public class GridPageCS : ContentPage
{
    public GridPageCS()
    {
        Grid grid = new Grid
        {
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(DeviceDisplay.Current.MainDisplayInfo.Width, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(100) }
            },
            ColumnDefinitions =
            {
                new ColumnDefinition()
            }
        };

        grid.Children.Add(new BoxView
        {
            Color = Colors.Green
        });
        grid.Children.Add(new Label
        {
            Text = "Row 0, Column 0",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center
        });

        Title = "Basic Grid demo";
        Content = grid;
    }
}

相关问题