简体   繁体   English

无法在我的课程中使用pictureBox.Image

[英]Cannot use pictureBox.Image in my class

I am making a game and I have a class called gameScripts . 我正在制作游戏,并且有一个名为gameScripts的类。 Inside gameScripts is a public void method called paintSquare . gameScripts内部是一个称为paintSquarepublic void方法。 When this method is called,the method uses 2 if statements,and depending on which one is true,the squares image will be changed accordingly. 调用此方法时,该方法使用2个if语句,并且根据哪一个为真,将相应更改正方形图像。

The problem is,when I try to use pictureBox.Image = Image.FromFile("cross.png"); 问题是,当我尝试使用pictureBox.Image = Image.FromFile("cross.png"); to change the picture to a cross, pictureBox.Image gets a red line under it with the error message " Error 2 'System.Windows.Forms.Control' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?) c:\\x\\x\\x\\x\\x\\x\\x\\gameScripts.cs " 要将图片更改为十字, pictureBox.Image在其下方显示一条红线,并显示错误消息“ Error 2 'System.Windows.Forms.Control' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?) c:\\x\\x\\x\\x\\x\\x\\x\\gameScripts.cs

I have tried including System.Drawing and System.Windows.Forms in my namespaces,but I still get this error. 我尝试在名称空间中包含System.Drawing和System.Windows.Forms,但仍然出现此错误。

Any help would be appreciated,thanks. 任何帮助,将不胜感激,谢谢。

The message 'ClassXXX' does not contain a definition for 'YYY' and no extension method 'YYY' accepting a first argument of type 'ClassXXX' could be found (are you missing a using directive or an assembly reference?) means literally what it says. 消息'ClassXXX' does not contain a definition for 'YYY' and no extension method 'YYY' accepting a first argument of type 'ClassXXX' could be found (are you missing a using directive or an assembly reference?)从字面上看意味着什么说。 Most likely in you code there is construction like this: 您的代码中最有可能是这样的构造:

myObject.YYY

but the class, the instance of which the myObject is, does not have a member with name YYY. 但是该类(其myObject的实例)没有名称为YYY的成员。

For example: 例如:

class MyClass {
     public string MyField;
}

...

var myObj = new MyClass();
myObj.MyField = "OK";
myObj.NotMyField = "FAIL"; // compiler will complain at this line

However, the compiler gets the list of available properties and methods by looking at the variable type. 但是,编译器通过查看变量类型来获取可用属性和方法的列表。 This may lead to the situation when the object itself have the member but compiler cannot see it as the variable is defined with a different type. 当对象本身具有成员但编译器无法看到它时,可能会导致这种情况,因为变量是用其他类型定义的。

Consider the following code fragment: 考虑以下代码片段:

class MyExtClass : MyClass {
     public string MyNewField;
}

...

MyClass myObj = new MyExtClass();
myObj.MyField = "OK";
myObj.MyNewField = "FAIL"; // compiler will complain at this line
                           // because MyClass does not have it

So in your code the pictureBox is seems to be defined as System.Windows.Forms.Control . 因此,在您的代码中, pictureBox似乎定义为System.Windows.Forms.Control So even if it is, in fact, System.Windows.Forms.PictureBox , compiler cannot know it and stops with the error. 因此,即使实际上是System.Windows.Forms.PictureBox ,编译器也无法识别它并因错误而停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM