简体   繁体   English

命名空间“系统”无法识别 - C#

[英]Namespace 'System' not recognized - C#

I am new to Visual Studio (2022) and was building my first windows form.我是 Visual Studio (2022) 的新手,正在构建我的第一个 windows 表单。 It worked perfect until today when I got this error where the namespace "System" is recognized as a local variable.直到今天我收到这个错误,其中名称空间“System”被识别为局部变量时,它一直运行良好。 I also searched the web for solutions but couldn't find any I was trying to get rounded corners to my form.我还搜索了 web 的解决方案,但找不到任何我试图为我的表单添加圆角的解决方案。 The part of code and screen-shot of error list is included below.下面是错误列表的部分代码和截图。

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));
       }

And this is Error List ss这是错误列表 ss

*Sorry if I am using the wrong terms and Thanks in advance *抱歉,如果我使用了错误的条款,请提前致谢

UPDATE: I tried re-installing Visual Studio (checked with the targeting component as well) and made a new project.更新:我尝试重新安装 Visual Studio(也检查了目标组件)并创建了一个新项目。 Still the Problem persists.问题仍然存在。

The compiler thinks System is a variable or a function. You probably have somewhere in the project a variable or a function named System.编译器认为System是一个变量或 function。您可能在项目的某处有一个名为 System 的变量或 function。 You can rename that variable or a function, or you can prepend this System use with global:: :您可以重命名该变量或 function,或者您可以在系统使用前加上global::

       Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));

There is probably a naming conflict where some variable, class or function in your code is called the same as the namespace System .可能存在命名冲突,代码中的某些变量 class 或 function 与名称空间System的名称相同。 There are multiple things you could do here.您可以在这里做很多事情。 The obvious thing would be to try and find and rename the thing that is conflicting with the namespace.显而易见的事情是尝试找到并重命名与命名空间冲突的东西。 If you can't you could do one of the following (chose the one that fits your use case and style best):如果不能,您可以执行以下操作之一(选择最适合您的用例和风格的操作):

  1. use the global keyword everywhere in your code when you want to access the System namespace:当你想访问System命名空间时,在你的代码中的任何地方使用global关键字:
Region = global::System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
  1. More convenient however would probably be using aliasing:然而更方便的可能是使用别名:
using Sys = global::System;

// ...

Region = Sys.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
  1. You may just consider importing the System.Drawing namespace so you don't have to type it everywhere in your code:您可能只考虑导入System.Drawing命名空间,这样您就不必在代码中的任何地方键入它:
using global::System.Drawing;

// ...

Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));

however this may not work if you have a custom class that is called Region too.但是,如果您也有一个名为Region的自定义 class,则这可能不起作用。 In this case we can use aliasing again.在这种情况下,我们可以再次使用别名。 For example a while ago I had to convert a Bitmap from System.Drawing to an AvaloniaUI Bitmap .例如,前一段时间我不得不将BitmapSystem.Drawing转换为 AvaloniaUI Bitmap In these conflict cases you can use using-aliasing in the following way:在这些冲突情况下,您可以按以下方式使用别名:

using ava = Avalonia.Media.Imaging;
using sys = System.Drawing;

// ...

public static ava::Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
    // ...
}

or if you use one of them all the time you could alias the one you use less often and just import the other one normally或者如果你一直使用其中一个,你可以给你不常使用的那个起个别名,然后正常导入另一个

using Avalonia.Media.Imaging;
using sys = System.Drawing;

// ...

public static Bitmap ToAvaloniaBitmap(this sys::Bitmap bm)
{
    // ...
}

Therefore aliasing is useful as it can spare you having to type all those (possibly very long) namespace names everywhere in your code.因此,别名很有用,因为它可以让您不必在代码中的任何地方键入所有这些(可能很长的)命名空间名称。

Another thing另一件事

if it isn't a naming conflict it might be in rare cases that your IntelliSense is a bit borked.如果不是命名冲突,则在极少数情况下,您的 IntelliSense 可能会有点乏味。 That depends if System is the only namespace it doesn't find or if it just underlines everything with red and even stops recognizing keywords like class , etc. In the latter case a quick clean rebuild of the solution or project reload or restarting VS might be everything needed to fix this.这取决于System是否是它找不到的唯一命名空间,或者它是否只是用红色强调一切,甚至停止识别关键字,如class等。在后一种情况下,快速清理解决方案或项目重新加载或重新启动 VS 可能是解决这个问题所需的一切。

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

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