0

我正在使用 C# 和 XAML 开发 Metro 应用程序,所以我需要一些帮助来将一些 XAML 代码转换为 C#,下面的代码在 XAML 中并且工作正常,我想在 C# 中执行此操作

<Grid x:Name="ObjRootGrid" Background="Black" Margin="248,198,227,182">
    <Grid.RowDefinitions>
        <RowDefinition Height="78*"/>
        <RowDefinition Height="101*"/>
        <RowDefinition Height="94*"/>
        <RowDefinition Height="111*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="111*"/>
        <ColumnDefinition Width="143*"/>
        <ColumnDefinition Width="147*"/>
        <ColumnDefinition Width="141*"/>
        <ColumnDefinition Width="141*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="61,49,0,0" Grid.Row="1" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBlock Grid.Column="2" HorizontalAlignment="Left" Margin="71,45,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBlock HorizontalAlignment="Left" Margin="68,45,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="106,58,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBlock HorizontalAlignment="Left" Margin="63,58,0,0" Grid.Row="3" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>

</Grid>

任何人都可以帮我解决这个问题,在此先感谢

4

1 回答 1

3
            var grid = new Grid() {
                Name = "ObjRootGrid",
                Background = new SolidColorBrush(Colors.Black),
                Margin = new Thickness(248, 198, 227, 182)
            };
            var rowDef1 = new RowDefinition();
            rowDef1.Height = new GridLength(78, GridUnitType.Star);
            grid.RowDefinitions.Add(rowDef1);
            //continue adding row definitions...
            var colDef1 = new ColumnDefinition();
            colDef1.Width = new GridLength(111, GridUnitType.Star);
            grid.ColumnDefinitions.Add(colDef1);
            //continue adding column definitions...

            var textBox1 = new TextBlock() {
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                Margin = new Thickness(61,49,0,0),
                TextWrapping = TextWrapping.Wrap,
                Text = "TextBlock",
                VerticalAlignment = System.Windows.VerticalAlignment.Top
            };
            Grid.SetColumn(textBox1, 1);
            Grid.SetRow(textBox1, 1);
            grid.Children.Add(textBox1);
            //continue adding text blocks...
于 2012-07-10T06:46:49.180 回答