直接从对象定义编辑行和列中的子项放置

时间:2017-07-25 09:32:36

标签: c# xamarin xamarin.forms

我有以下对象:

new Grid {
    RowDefinitions =
    {
        new RowDefinition { Height = new GridLength(3, GridUnitType.Star) },
        new RowDefinition { Height = new GridLength(2, GridUnitType.Star) },
        new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }
    },
    ColumnDefinitions =
    {
        new ColumnDefinition { Width = new GridLength(3, GridUnitType.Star) },    
        new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
    },
    Children =
    {
        new Label { Text = "TRIP #5", FontFamily = Device.OnPlatform(null, "latoblack.ttf#Lato Black", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start, Margin = new Thickness(0, 0, 0, 10) },
        new Label { Text = "Started 24/07/2017", FontFamily = Device.OnPlatform(null, "latolight.ttf#Lato Light", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start },
        new Label { Text = "Is currently in progress", FontFamily = Device.OnPlatform(null, "latolight.ttf#Lato Light", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start }
    }
}

我想将标签放在位置(0,0),(0,1),(1,1)中,而不必使用Children.Add。更具体地说,我想将标签的位置设置为我直接从对象定义中提到的位置。我怎么能这样做?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用Grid静态方法Grid.SetRow, Grid.SetColumn等,如下所示:

        var label = new Label { Text = "TRIP #5", FontSize = 16, TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Start, Margin = new Thickness(0, 0, 0, 10) };
        var grid = new Grid {
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(3, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(2, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }
            },
            ColumnDefinitions =
            {
                new ColumnDefinition { Width = new GridLength(3, GridUnitType.Star) },    
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
            },
            Children =
            {
                label,
            }
        };

        grid.Padding = new Thickness(0,20,0,0);


        Grid.SetRow(label, 0);
        Grid.SetColumn(label, 0);
        Grid.SetColumnSpan(label,2);
        Grid.SetRowSpan(label, 3);

        this.Content = grid;