将此UniformGrid xaml语言转换为C#
大家好,我想把这个xaml代码转换成c代码将此UniformGrid xaml语言转换为C#,c#,wpf,xaml,C#,Wpf,Xaml,大家好,我想把这个xaml代码转换成c代码 请帮助我使用循环来节省内存或行 请帮帮我 <UniformGrid Width="500" Height="500" x:Name="ChessBoard" VerticalAlignment="Center" HorizontalAlignment="Center"> <Grid x:Name="Grid1" Background="Yellow" /> <Grid x:Name="Gri
请帮助我使用循环来节省内存或行 请帮帮我
<UniformGrid Width="500" Height="500" x:Name="ChessBoard" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid x:Name="Grid1" Background="Yellow" />
<Grid x:Name="Grid2" Background="Black" />
<Grid x:Name="Grid3" Background="Yellow" />
<Grid x:Name="Grid4" Background="Black" />
<Grid x:Name="Grid5" Background="Yellow" />
<Grid x:Name="Grid6" Background="Black" />
<Grid x:Name="Grid7" Background="Yellow" />
<Grid x:Name="Grid8" Background="Black" />
<Grid x:Name="Grid9" Background="Yellow" />
</UniformGrid>
请
UniformGrid ChessBoard = new UniformGrid();
ChessBoard.Width = 500;
ChessBoard.Height = 500;
ChessBoard.HorizontalAlignment = HorizontalAlignment.Center;
ChessBoard.VerticalAlignment = VerticalAlignment.Center;
Grid chressBox = new Grid();
SolidColorBrush yell = new SolidColorBrush(Colors.Yellow);
SolidColorBrush blk = new SolidColorBrush(Colors.Black);
for (int ii = 1; ii <= 9; ii++)
{
if (ii % 2 == 0)
{
chressBox.Background = blk;
chressBox.Name = "Grid" + ii.ToString();
ChessBoard.Children.Add(chressBox);
}
else
{
chressBox.Background = yell;
chressBox.Name = "Grid" + ii.ToString();
ChessBoard.Children.Add(chressBox);
}
}
LayoutRoot.Children.Add(ChessBoard);
UniformGrid棋盘=新的UniformGrid();
棋盘宽度=500;
棋盘高度=500;
ChessBoard.HorizontalAlignment=HorizontalAlignment.Center;
ChessBoard.VerticalAlignment=VerticalAlignment.Center;
Grid chressBox=新网格();
SolidColorBrush yell=新的SolidColorBrush(Colors.Yellow);
SolidColorBrush blk=新的SolidColorBrush(Colors.Black);
对于(intii=1;ii对于一个完整的棋盘,你可以这样做
Brush defaultBrush = new SolidColorBrush(Colors.Yellow);
Brush alternateBrush = new SolidColorBrush(Colors.Black);
for (int x = 0; x < 8; ++x)
{
for (int y = 0; y < 8; ++y)
{
Grid cell = new Grid();
cell.Background = (y+x) % 2 == 0 ? defaultBrush : alternateBrush;
ChessBoard.Children.Add(cell);
}
}
Brush defaultBrush=新的SolidColorBrush(Colors.Yellow);
Brush alternateBrush=新的SolidColorBrush(颜色为黑色);
对于(int x=0;x<8;++x)
{
对于(int y=0;y<8;++y)
{
网格单元=新网格();
cell.Background=(y+x)%2==0?默认笔刷:alternateBrush;
棋盘。子对象。添加(单元格);
}
}
这是一个只有一个for循环的版本
Brush defaultBrush = new SolidColorBrush(Colors.Yellow);
Brush alternateBrush = new SolidColorBrush(Colors.Black);
for (int i = 0; i < 64; ++i)
{
Grid cell = new Grid();
cell.Background = (i + i / 8) % 2 == 0 ? defaultBrush : alternateBrush;
ChessBoard.Children.Add(cell);
}
Brush defaultBrush=新的SolidColorBrush(Colors.Yellow);
Brush alternateBrush=新的SolidColorBrush(颜色为黑色);
对于(int i=0;i<64;++i)
{
网格单元=新网格();
cell.Background=(i+i/8)%2==0?默认笔刷:alternateBrush;
棋盘。子对象。添加(单元格);
}
此代码假设您有一个棋盘
定义为UniformGrid,如示例中所示。“使用循环来节省内存或行”??似乎没有理由取代它。哇,它的工作,哦,是的,如何创建或命名网格?谢谢,是的:D@yozawiratama,命名单元格的一种方法是使用控制变量。例如,在第二个代码段中,可以添加cell.name=string.Format(“cell{0:00}”,i);
在循环中。这将按编号Cell00、Cell01、Cell02等命名每个单元格。