C# 动态创建图像-当前上下文中不存在

C# 动态创建图像-当前上下文中不存在,c#,image,C#,Image,我正在尝试用C#的WPF创建一个古怪的游戏。我有点傻。在for循环中,我试图将“I”的编号添加到“Image”中。我得到以下错误: "Error The name 'Image1' does not exist in the current context" “Image2”等的情况也是如此。我正在尝试将图像集成到StackPanel中 谢谢你的帮助:) 公共部分类主窗口:窗口 { Image[]ImageArray=新图像[50]; 公共主窗口() { Moleini=摩尔分数[1];

我正在尝试用C#的WPF创建一个古怪的游戏。我有点傻。在for循环中,我试图将“I”的编号添加到“Image”中。我得到以下错误:

 "Error The name 'Image1' does not exist in the current context" 
“Image2”等的情况也是如此。我正在尝试将图像集成到StackPanel中

谢谢你的帮助:)

公共部分类主窗口:窗口
{
Image[]ImageArray=新图像[50];
公共主窗口()
{
Moleini=摩尔分数[1];
初始化组件();
//字符串ImageName=“Image”;

对于(int i=0;i> p>代码图像图像=新图像();可能会导致问题。您应该考虑使用<代码>图像图像=新图像();< /代码>,在变量名

< p>代码>图像图像=新图像();可能导致问题。您应该考虑使用<代码>图像-=新图像()。;
,变量名上不带大写字母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));
                    ImageArray[1].Source = MoleImage;            
                }
                //If Random Number does not equal 1
                if (num != 1)
                {
                    ImageSource hole = new BitmapImage(new Uri(ImgHole));
                    ImageArray[1].Source = hole;
                }

                //If Random Number is "2" Then Image will display
                if (num == 2)
                {
                    ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
                    ImageArray[2].Source = MoleImage;
                }
    }

使用此更新的代码:

            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));
                    ImageArray[1].Source = MoleImage;            
                }
                //If Random Number does not equal 1
                if (num != 1)
                {
                    ImageSource hole = new BitmapImage(new Uri(ImgHole));
                    ImageArray[1].Source = hole;
                }

                //If Random Number is "2" Then Image will display
                if (num == 2)
                {
                    ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
                    ImageArray[2].Source = MoleImage;
                }
    }

除非您省略了代码,
Image1
Image2
之前未声明。在
dispatchermer\u Tick
范围的上下文中使用这些变量将导致编译时错误

我想您是想引用
ImageArray

// instead of this
Image1.Source = MoleImage;
// you want this
ImageArray[1].Source = MoleImage;

除非您省略了代码,
Image1
Image2
之前未声明。在
dispatchermer\u Tick
范围的上下文中使用这些变量将导致编译时错误

我想您是想引用
ImageArray

// instead of this
Image1.Source = MoleImage;
// you want this
ImageArray[1].Source = MoleImage;

您是否考虑过在游戏中使用MVVM设计模式?它更适合WPF的技术,因为它将数据层和UI层分开,就像WPF的XAML和绑定系统那样,这将使这变得更容易

我记得一个关于扫雷游戏的类似问题,所以我从这里开始

从创建
Mole
对象开始。Mole有3个属性:
RowIndex
ColumnIndex
IsUp
属性

现在您需要一个模板来绘制
Mole
对象。为
local:Mole
对象创建一个
DataTemplate
,并使用您的图像来绘制它。如果
IsUp=True
,则可以使用
DataTrigger
来绘制Mole图片,如果
IsUp=False
,则可以使用孔图像

现在在您的代码隐藏中,创建一个
Mole
对象列表,并初始化它们的默认值。这意味着两个循环将通过创建
Mole
对象和设置它们的行/列索引来完成

要绘制列表,请在XAML中使用
ItemsControl
。将
ItemsControl.ItemsPanelTemplate
更改为
Grid
,并将
ItemsControl.itemsContainerStyle
Grid.Row
ColumnIndex
属性绑定到上的
行索引和
ColumnIndex
属性ode>摩尔
对象

最后,启动一个计时器,用
IsUp=false
将列表中随机
Mole
对象的
IsUp
属性随机更改为
true
。当将其更改为true时,还启动第二个计时器,该计时器将在随机时间后更改
IsUp=false

添加分数应该相当容易。向
Mole
对象添加
ICommand HitMoleCommand
,该对象返回
RelayCommand
,该命令在
IsUp=True
时启用,并在那里执行一些逻辑(计算点、更改
IsUp=False
和取消计时器等)

但是无论如何,
Image1
不是您的
MainWindow
类的属性,这就是为什么您不能从dispatcher代码访问它。仅仅创建一个对象并给它一个名称并不会将其作为属性存储在窗口中,就像在运行项目之前在XAML中创建一个对象并给它一个名称一样。您需要或者在类上的某个位置访问图像,如在
ImageArray
对象中


我看到你在我写这篇文章的时候就找到了答案,但我还是把它贴出来,因为我强烈觉得如果你在使用WPF,你至少应该了解MVVM设计模式,即使你没有选择使用它:)

您是否考虑过在游戏中使用MVVM设计模式?它更适合WPF的技术,因为它可以像WPF的XAML和绑定系统那样将数据层和UI层分开,这将使这一点变得更容易

我记得一个关于扫雷游戏的类似问题,所以我从这里开始

从创建
Mole
对象开始。Mole有3个属性:
RowIndex
ColumnIndex
IsUp
属性

现在您需要一个模板来绘制
Mole
对象。为
local:Mole
对象创建一个
DataTemplate
,并使用您的图像来绘制它。如果
IsUp=True
,则可以使用
DataTrigger
来绘制Mole图片,如果
IsUp=False
,则可以使用孔图像

现在在您的代码隐藏中,创建一个
Mole
对象列表,并初始化它们的默认值。这意味着两个循环将通过创建
Mole
对象和设置它们的行/列索引来完成

要绘制列表,请在XAML中使用
ItemsControl
。将
ItemsControl.ItemsPanelTemplate
更改为
Grid
,并将
ItemsControl.itemsContainerStyle
Grid.Row
ColumnIndex
属性绑定到上的
行索引和
ColumnIndex
属性ode>摩尔
对象

最后,启动一个计时器,随机更改随机