简体   繁体   English

命名空间的怪异问题

[英]Weird issue with namespace

I'm trying to use the System.Drawing.Color namespace. 我正在尝试使用System.Drawing.Color命名空间。 I'm not able to define it at the top of the class: 我无法在课程顶部定义它:

在此处输入图片说明

However, I can reference it within the class. 但是,我可以在课堂上引用它。 That is, I can use this line of code, and it works: 也就是说,我可以使用以下代码行,并且可以正常工作:

txtBox.BackColor = System.Drawing.Color.LightPink;

... but I'd rather just be able to do this: ...但是我宁愿能够做到这一点:

txtBox.BackColor = Color.LightPink;

If it's a matter of a missing reference/dll, why am I able to make reference to System.Drawing.Color in my code? 如果是缺少引用/ dll的问题,为什么我可以在代码中引用System.Drawing.Color?

That's because it isn't a namespace. 那是因为它不是名称空间。 System.Drawing is the namespace, and Color is a structure ( struct ). System.Drawing是名称空间,而Color是结构( struct )。

EDIT: Additionally, I'd advise to use a product such as ReSharper , which can correct stuff like this almost automagically. 编辑:另外,我建议您使用ReSharper之类的产品,该产品几乎可以自动纠正此类问题。 ReSharper rules! ReSharper规则!

System.Drawing.Color is a struct. System.Drawing.Color是一个结构。 Namespace is System.Drawing 命名空间为System.Drawing

您是否在项目References添加了对System.DrawingReferences

You have to add it to your references - somehow it is missing. 您必须将其添加到参考中-某种程度上它是丢失的。

then you have to declare using System.Drawing; 那么您必须using System.Drawing;进行声明using System.Drawing; in the top portion of your C# code then you can call Color.LightPink; 在C#代码的顶部,您可以调用Color.LightPink;

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

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