将此 UniformGrid xaml 语言转换为 C#

发布于 2024-11-03 15:13:05 字数 1729 浏览 0 评论 0原文

大家好,我想将此 xaml 代码转换为 C# 代码 请帮助我使用循环来节省内存或线路 请帮助我

<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);

创建它,但仍然错误

Hi everyone i want to convert this xaml code to c# code
please help me using a looping to save memory or line
please help me

<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>

please

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);

itry to create that, but still wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对于完整的棋盘,您可以执行类似的操作

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

这是一个仅包含单个 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);
  }

此代码假设您有一个 ChessBoard 定义为 UniformGrid,如示例中所示。

For a full chessboard you could do something like this

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

Here is a version with only a single for loop

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

This code assumes you have a ChessBoard defined as a UniformGrid as in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文