[英]Cannot use pictureBox.Image in my class
我正在制作游戏,并且有一个名为gameScripts
的类。 在gameScripts
内部是一个称为paintSquare
的public void
方法。 调用此方法时,该方法使用2个if
语句,并且根据哪一个为真,将相应更改正方形图像。
问题是,当我尝试使用pictureBox.Image = Image.FromFile("cross.png");
要将图片更改为十字, 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
”
我尝试在名称空间中包含System.Drawing和System.Windows.Forms,但仍然出现此错误。
任何帮助,将不胜感激,谢谢。
消息'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?)
从字面上看意味着什么说。 您的代码中最有可能是这样的构造:
myObject.YYY
但是该类(其myObject的实例)没有名称为YYY的成员。
例如:
class MyClass {
public string MyField;
}
...
var myObj = new MyClass();
myObj.MyField = "OK";
myObj.NotMyField = "FAIL"; // compiler will complain at this line
但是,编译器通过查看变量类型来获取可用属性和方法的列表。 当对象本身具有成员但编译器无法看到它时,可能会导致这种情况,因为变量是用其他类型定义的。
考虑以下代码片段:
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
因此,在您的代码中, pictureBox
似乎定义为System.Windows.Forms.Control
。 因此,即使实际上是System.Windows.Forms.PictureBox
,编译器也无法识别它并因错误而停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.