How can I create an Grid with its first cell to be square in .Net Maui?

Viewed 92

I want to create a Grid with its first cell to be square, i.e. its height equals its width which is decided by screen width. I was thinking something like below but it didn't work.

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

Yes, you can achieve this in code.

I created a demo ,you can refer to the following code:

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;
    }
}
Related