英文:
Problems with XAML grids
问题
我正在尝试学习C#,所以我决定跟随一个YouTube教程,教你如何使用XAML和C#创建贪吃蛇游戏。
这是视频,我在46分钟处停了下来
一切都很顺利,直到我们完成了网格的设置,然后运行了代码。结果如下所示:
而我的结果却是这样的:
。
我相当确定我没有漏掉任何东西,但也许真正了解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 mine was like this . 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:
<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;
}
}
}
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
图像位于其中间:
您可以将图像设置为 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:
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:
<UniformGrid x:Name="GameGrid"
Width="400"
Height="267"
Background="{StaticResource GridBackgroundColor }"
SnapsToDevicePixels="True"
RenderOptions.BitmapScalingMode="HighQuality">
</UniformGrid>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论