我正在尝试在 C# 中的 WPF 中创建 WhackaMole 游戏。我有点菜鸟。在 for 循环中,我试图将“i”的数量添加到“图像”中。我收到以下错误:
"Error The name 'Image1' does not exist in the current context"
'Image2' 等也是一样的。我正在尝试将图像集成到 StackPanel 中。
谢谢您的帮助 :)
public partial class MainWindow : Window
{
Image[] ImageArray = new Image[50];
public MainWindow()
{
Moleini = MoleScore[1];
InitializeComponent();
//string ImageName = "Image";
for (int i = 0; i <= 8; i++)
{
Image Image = new Image();
ImageArray[i] = Image;
Image.Name = "Image" + i.ToString();
}
////Create Images
//for (int i = 0; i <= 8; i++)
//{
// StackPanel1.Children.Add(CreateImage(i));
//}
//Dispacher for Mole to Appear
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Start();
//Dispacher for Full Game Time
System.Windows.Threading.DispatcherTimer endGame = new System.Windows.Threading.DispatcherTimer();
endGame.Tick += new EventHandler(endGame_Tick);
endGame.Interval = TimeSpan.FromSeconds(5);
endGame.Start();
}
////Create Image
//public Image CreateImage(int i)
//{
//}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//Random Number Generator
Random rnd = new Random();
int num = rnd.Next(1, 9);
//If Random Number is "1" Then Image will display
if (num == 1)
{
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Image1.Source = MoleImage;
}
//If Random Number does not equal 1
if (num != 1)
{
ImageSource hole = new BitmapImage(new Uri(ImgHole));
Image1.Source = hole;
}
//If Random Number is "2" Then Image will display
if (num == 2)
{
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Image2.Source = MoleImage;
}
}