[英]Namespace 'System' not recognized - C#
我是 Visual Studio (2022) 的新手,正在构建我的第一个 windows 表单。 直到今天我收到这个错误,其中名称空间“System”被识别为局部变量时,它一直运行良好。 我还搜索了 web 的解决方案,但找不到任何我试图为我的表单添加圆角的解决方案。 下面是错误列表的部分代码和截图。
public partial class Dashboard : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // width of ellipse
int nHeightEllipse // height of ellipse
);
public Dashboard()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = **System**.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
*抱歉,如果我使用了错误的条款,请提前致谢
更新:我尝试重新安装 Visual Studio(也检查了目标组件)并创建了一个新项目。 问题仍然存在。
编译器认为System
是一个变量或 function。您可能在项目的某处有一个名为 System 的变量或 function。 您可以重命名该变量或 function,或者您可以在系统使用前加上global::
:
Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
可能存在命名冲突,代码中的某些变量 class 或 function 与名称空间System
的名称相同。 您可以在这里做很多事情。 显而易见的事情是尝试找到并重命名与命名空间冲突的东西。 如果不能,您可以执行以下操作之一(选择最适合您的用例和风格的操作):
System
命名空间时,在你的代码中的任何地方使用global
关键字:Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
using Sys = global::System;
// ...
Region = Sys.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
System.Drawing
命名空间,这样您就不必在代码中的任何地方键入它:using global::System.Drawing;
// ...
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
但是,如果您也有一个名为Region
的自定义 class,则这可能不起作用。 在这种情况下,我们可以再次使用别名。 例如,前一段时间我不得不将Bitmap
从System.Drawing
转换为 AvaloniaUI Bitmap
。 在这些冲突情况下,您可以按以下方式使用别名:
using ava = Avalonia.Media.Imaging;
using sys = System.Drawing;
// ...
public static ava::Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
// ...
}
或者如果你一直使用其中一个,你可以给你不常使用的那个起个别名,然后正常导入另一个
using Avalonia.Media.Imaging;
using sys = System.Drawing;
// ...
public static Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
// ...
}
因此,别名很有用,因为它可以让您不必在代码中的任何地方键入所有这些(可能很长的)命名空间名称。
另一件事
如果不是命名冲突,则在极少数情况下,您的 IntelliSense 可能会有点乏味。 这取决于System
是否是它找不到的唯一命名空间,或者它是否只是用红色强调一切,甚至停止识别关键字,如class
等。在后一种情况下,快速清理解决方案或项目重新加载或重新启动 VS 可能是解决这个问题所需的一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.