Windows phone 7 以编程方式生成布局(XAML)

Windows phone 7 以编程方式生成布局(XAML),windows-phone-7,xaml,Windows Phone 7,Xaml,如何以编程方式生成布局(XAML) 假设我有一个循环。每次之后,我想用我得到的值生成这个: <Grid Height="150" Name="grid1"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Wi

如何以编程方式生成布局(XAML)

假设我有一个循环。每次之后,我想用我得到的值生成这个:

                <Grid Height="150" Name="grid1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <Image Height="150" Name="image1" Stretch="Fill" Width="200" Grid.Column="1" Source="/Ilm;component/Images/testicon.png" HorizontalAlignment="Right" VerticalAlignment="Top" />
                    <TextBlock Height="51" Name="textBlock1" Text="Paris" Margin="12,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="40" />
                    <TextBlock FontSize="40" Height="51" HorizontalAlignment="Left" Margin="13,75,0,0" Name="textBlock2" Text="19°C" VerticalAlignment="Top" />
                </Grid>

要向网格中添加新行,需要添加新行定义,然后添加新控件。它将类似于以下逻辑:

rowNumber = 0; // Set the current row that you are adding 
grid1.RowDefinitions.Add(new RowDefinition());

Image img = new Image();
img.Height = 150;
img.Name = "image1";
img.Stretch = Stretch.Fill;
img.Width = 200;
img.HorizontalAlignment = HorizontalAlignment.Right;
img.VerticalAlignment = VerticalAlignment.Top;
// Set your image properties
img.SetValue(Grid.RowProperty, rowNumber);
img.SetValue(Grid.ColumnProperty, 1);
grid1.Children.Add(img);

TextBlock tb1 = new TextBlock();
// Set your text block properties
tb1.SetValue(Grid.RowProperty, rowNumber);
tb1.SetValue(Grid.ColumnProperty, 0);
grid1.Children.Add(tb1);

TextBlock tb2 = new TextBlock();
// Set your text block properties
tb2.SetValue(Grid.RowProperty, rowNumber);
tb2.SetValue(Grid.ColumnProperty, 0);
grid1.Children.Add(tb2);
您需要将要设置的属性放在我有注释的位置,并提供正确的行号(从零开始)

要添加整个内容

Grid grid1 = new Grid();
grid1.Height = 150;
grid1.Name = "grid1";
parentControl.Children.Add(grid1); // Note: parentControl is whatever control you are added this to
grid1.ColumnDefinitions.Add(new ColumnDefinition());
grid1.ColumnDefinitions.Add(new ColumnDefinition { Width = 200});

// From here insert the code for adding a row that is provided above

就这样。你只需要填写我没有提供的属性。请记住,您的变量
parentControl
将不同。您尚未提供要将这些控件添加到的控件,因此不清楚该控件是什么。

要将新行添加到网格中,您需要添加新行定义,然后添加新控件。它将类似于以下逻辑:

rowNumber = 0; // Set the current row that you are adding 
grid1.RowDefinitions.Add(new RowDefinition());

Image img = new Image();
img.Height = 150;
img.Name = "image1";
img.Stretch = Stretch.Fill;
img.Width = 200;
img.HorizontalAlignment = HorizontalAlignment.Right;
img.VerticalAlignment = VerticalAlignment.Top;
// Set your image properties
img.SetValue(Grid.RowProperty, rowNumber);
img.SetValue(Grid.ColumnProperty, 1);
grid1.Children.Add(img);

TextBlock tb1 = new TextBlock();
// Set your text block properties
tb1.SetValue(Grid.RowProperty, rowNumber);
tb1.SetValue(Grid.ColumnProperty, 0);
grid1.Children.Add(tb1);

TextBlock tb2 = new TextBlock();
// Set your text block properties
tb2.SetValue(Grid.RowProperty, rowNumber);
tb2.SetValue(Grid.ColumnProperty, 0);
grid1.Children.Add(tb2);
您需要将要设置的属性放在我有注释的位置,并提供正确的行号(从零开始)

要添加整个内容

Grid grid1 = new Grid();
grid1.Height = 150;
grid1.Name = "grid1";
parentControl.Children.Add(grid1); // Note: parentControl is whatever control you are added this to
grid1.ColumnDefinitions.Add(new ColumnDefinition());
grid1.ColumnDefinitions.Add(new ColumnDefinition { Width = 200});

// From here insert the code for adding a row that is provided above

就这样。你只需要填写我没有提供的属性。请记住,您的变量
parentControl
将不同。您还没有提供要将这些代码添加到的控件,因此不清楚该控件是什么。

一个选项是将代码中的所有代码都放在后面


另一个更为传统的选择是使用。这将允许通过在XAML中保留可视化布局而不是代码隐藏来更清晰地划分职责。

一个选项是将所有代码作为一个组件放置在代码隐藏中


另一个更为传统的选择是使用。这将通过在XAML中保留可视化布局而不是代码来实现更清晰的职责分离。

您想在其中添加新行还是生成整个内容?我想生成整个内容。。。需要多少就有多少。好的,看看下面我的答案。我将其更新为添加网格。是否要向其中添加新行或生成整个内容?我要生成整个内容。。。需要多少就有多少。好的,看看下面我的答案。我更新了它,添加了一个网格。是的,当然。我应该提到他的编程方法肯定不是最好的。我只是想把它扔出去,以防OP不知道ItemsControl。是的,当然。我应该提到他的编程方法肯定不是最好的。我只是想把它扔出去,以防OP不知道ItemsControl。当我试图将网格添加到“parentControl”时,它会说“参数不正确”。并且没有什么更有价值的方法来修复错误。。。当然比你长的回答更重要。。也许我应该找一下控件安装的项目。。因为这对我来说太复杂了…好吧,一个小时后我明白了。。。我让你的代码为我的项目工作。你是老板,贝索斯!!!当我尝试将网格添加到“parentControl”时,它会显示“参数不正确”。并且没有什么比修复错误更有价值的了。。。当然比你长的回答更重要。。也许我应该找一下控件安装的项目。。因为这对我来说太复杂了…好吧,一个小时后我明白了。。。我让你的代码为我的项目工作。你是老板,贝索斯!!!