XAML网格的问题

huangapple go评论91阅读模式
英文:

Problems with XAML grids

问题

我正在尝试学习C#,所以我决定跟随一个YouTube教程,教你如何使用XAML和C#创建贪吃蛇游戏。
这是视频,我在46分钟处停了下来
一切都很顺利,直到我们完成了网格的设置,然后运行了代码。结果如下所示:
XAML网格的问题
而我的结果却是这样的:
XAML网格的问题
我相当确定我没有漏掉任何东西,但也许真正了解XAML和C#的人会注意到差异,以下是此页面的XAML和C#代码:

<Window x:Class="SnakeGame2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SnakeGame2"
        mc:Ignorable="d"
        Title="Snake" Height="500" Width="800"
        Background="{StaticResource BackgroundColor}"
        Foreground="{StaticResource TextColor}"
        FontFamily="{StaticResource MainFont}"
        WindowStartupLocation="CenterScreen"
        Icon="Assets/icon.ico">


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="Scoretext"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Text="Score: 0"
                   FontSize="22"
                   Margin="10"/>
        <Border x:Name="GridBorder"
                Grid.Row="1"
                BorderBrush="{StaticResource GridLinecolor}"
                BorderThickness="1.5"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                RenderOptions.EdgeMode="Aliased">
            <UniformGrid x:Name="GameGrid"
                         Width="400"
                         Height="400"
                         Background="{StaticResource GridBackgroundColor }"
                         SnapsToDevicePixels="True"
                         RenderOptions.BitmapScalingMode="HighQuality">
            </UniformGrid>
        </Border>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SnakeGame2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly int rows = 10, cols = 15;
        private readonly Image[,] gridImages;
        public MainWindow()
        {
            InitializeComponent();
            gridImages = SetupGrid();
        }

        private Image[,] SetupGrid()
        {
            Image[,] images = new Image[rows, cols];
            GameGrid.Rows = rows;
            GameGrid.Columns = cols;    
            
            for (int r=0; r < rows; r++) 
            { 
                for(int c=0; c < cols; c++)
                {
                    Image image = new Image
                    {
                        Source = Images.Empty
                    };
                    images[r, c] = image;
                    GameGrid.Children.Add(image);
                }
            }
            return images;
        }
    }
}

感谢您的时间。

英文:

I'm trying to learn c# so I decided to follow a tutorial on youtube that teaches you how to create the snake game using xaml and c#.
this is the video, I stopped at 46 min
Everything ok until, after concluding the setup of the grid we runned the code. While is results are like this XAML网格的问题 mine was like this XAML网格的问题. I'm pretty sure that I haven't missed anything but maybe anyone who actually knows xaml and c# would notice the diferences, both xaml and c# code for this page:

&lt;Window x:Class=&quot;SnakeGame2.MainWindow&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
        xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
        xmlns:local=&quot;clr-namespace:SnakeGame2&quot;
        mc:Ignorable=&quot;d&quot;
        Title=&quot;Snake&quot; Height=&quot;500&quot; Width=&quot;800&quot;
        Background=&quot;{StaticResource BackgroundColor}&quot;
        Foreground=&quot;{StaticResource TextColor}&quot;
        FontFamily=&quot;{StaticResource MainFont}&quot;
        WindowStartupLocation=&quot;CenterScreen&quot;
        Icon=&quot;Assets/icon.ico&quot;&gt;


    &lt;Grid&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height=&quot;auto&quot;/&gt;
            &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;TextBlock x:Name=&quot;Scoretext&quot;
                   HorizontalAlignment=&quot;Center&quot;
                   VerticalAlignment=&quot;Center&quot;
                   Text=&quot;Score: 0&quot;
                   FontSize=&quot;22&quot;
                   Margin=&quot;10&quot;/&gt;
        &lt;Border x:Name=&quot;GridBorder&quot;
                Grid.Row=&quot;1&quot;
                BorderBrush=&quot;{StaticResource GridLinecolor}&quot;
                BorderThickness=&quot;1.5&quot;
                HorizontalAlignment=&quot;Center&quot;
                VerticalAlignment=&quot;Center&quot;
                RenderOptions.EdgeMode=&quot;Aliased&quot;&gt;
            &lt;UniformGrid x:Name=&quot;GameGrid&quot;
                         Width=&quot;400&quot;
                         Height=&quot;400&quot;
                         Background=&quot;{StaticResource GridBackgroundColor }&quot;
                         SnapsToDevicePixels=&quot;True&quot;
                         RenderOptions.BitmapScalingMode=&quot;HighQuality&quot;&gt;
            &lt;/UniformGrid&gt;
        &lt;/Border&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SnakeGame2
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        private readonly int rows = 10, cols = 15;
        private readonly Image[,] gridImages;
        public MainWindow()
        {
            InitializeComponent();
            gridImages = SetupGrid();
        }

        private Image[,] SetupGrid()
        {
            Image[,] images = new Image[rows, cols];
            GameGrid.Rows = rows;
            GameGrid.Columns = cols;    
            
            for (int r=0; r &lt; rows; r++) 
            { 
                for(int c=0; c &lt; cols; c++)
                {
                    Image image = new Image
                    {
                        Source = Images.Empty
                    };
                    images[r, c] = image;
                    GameGrid.Children.Add(image);
                }
            }
            return images;
        }
    }
}

Thank you for your time

答案1

得分: 2

您已将行数减少到10,在视频中有15行,但您没有改变网格的高度,仍然是400像素。

  • 将行数更改回15,它应该看起来像这个示例:
private readonly int rows = 15, cols = 15;

通常,这只会导致每个单元格的矩形,高度为40像素(40/10),宽度约为27像素(40/15)。但在您的情况下,您正在使用图像来渲染单元格背景,默认情况下,该图像会保持其纵横比以适应单元格。如果有帮助的话,在这个图像中,绿色部分是单元格,但您可以看到您的 Empty.png 图像位于其中间:

XAML网格的问题

您可以将图像设置为 Stretch,但这不会看起来与视频中的统一方形单元格一样。当使用固定图像进行渲染时,如果您要更改行数,通常会调整网格的高度,基本上我们只需要保持单元格图像的纵横比。

要解决此问题,请尝试将图像设置为拉伸:

Image image = new Image
{
    Source = Images.Empty,
    Stretch = Stretch.Fill
};

或者将高度更改为267像素:

<UniformGrid x:Name="GameGrid"
             Width="400"
             Height="267"
             Background="{StaticResource GridBackgroundColor}"
             SnapsToDevicePixels="True"
             RenderOptions.BitmapScalingMode="HighQuality">
</UniformGrid>
英文:

You have reduced the row count to 10, in the video it has 15, but you have not altered the height of the grid, that is still 400px.

  • Change the number of rows back to 15 and it should look like the example:

    private readonly int rows = 15, cols = 15;
    

Normally this just results in a rectangle for each cell with a height of 40px (40/10) and a width of ~27px (40/15), but in your case you are using an image to render the cell background and by default that image is maintaining it's aspect ratio to fit within the cell. If it helps, in this image, the green is the cell, but you can see your Empty.png image in the middle of that:

XAML网格的问题

You could either set the image to Stretch, but that will not look the same uniform square shape cells as in the video. When using fixed images for rendering we would normally adjust the height of the grid if you are altering the number of the rows, basically we just need to maintain the aspect ratio of the cell images.

To fix this, try setting the images to stretch:

Image image = new Image
{
    Source = Images.Empty,
    Stretch = Stretch.Fill
};

Or change the height to 267px:

&lt;UniformGrid x:Name=&quot;GameGrid&quot;
             Width=&quot;400&quot;
             Height=&quot;267&quot;
             Background=&quot;{StaticResource GridBackgroundColor }&quot;
             SnapsToDevicePixels=&quot;True&quot;
             RenderOptions.BitmapScalingMode=&quot;HighQuality&quot;&gt;
&lt;/UniformGrid&gt;

huangapple
  • 本文由 发表于 2023年7月17日 23:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定