XAML网格问题:贪吃蛇游戏实现与教程效果不符求助
贪吃蛇游戏XAML界面与教程效果不一致排查求助
我正在学习C#,跟着YouTube上一则使用XAML和C#开发贪吃蛇游戏的教程实操(进度至46分钟)。完成网格设置并运行代码后,发现界面效果和教程不一致:
教程界面效果:
我的界面效果:
我确认没有遗漏任何步骤,现将当前的XAML和C#代码附上,恳请熟悉XAML和C#的开发者帮忙排查差异:
XAML代码
<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>
C#代码
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; } } }
感谢您的宝贵时间!
内容的提问来源于stack exchange,提问作者André Vieira
相关产品推荐
相关产品推荐

