type resources does not exist in namespace error

2020-02-23 07:30发布

问题:

Got another problem. I had almost finished my project but felt like I had to change the namespace of the project. But instead of using refactor --> rename , I selected my namespace, edit --> find and replace and replaced my whole solution with a different namespace (so everywhere in the solution where for example the namespace or text "name1" exists, is now changed into "name2").

But now I get around 16 errors all saying the same thing: "The type or namespace "resources" does not exist in namespace name1".

     this.centerPanel.BackgroundImage = global::login_DragDrop.Properties.Resources.oeftest;

So for example in this line of code (mainly in the designer.cs) resources is colored red?

Any hints on how to fix this?

回答1:

You need to run the code generation on the resource file to get the correct namespace there as well.

Follow these steps:

  • Open the project properties (right click > properties)
    • click the Application section
    • Enter your prefered Default namespace
    • Close and save your properties
  • navigate in the solution explorer to your Resouces.resx file (under Properties)
    • Right-click the Resources.resx file
    • Choose 'Run Custom Tool' (or alt+L)

This runs the code generation for the resource files again. The generated code makes that you have strongly typed access to the values in your resource strings.



回答2:

Run Custom Tool did not work for me. But I found if I added a resource the problem fixed itself.

Follow these steps:

  • Open the project properties (right click > properties)
  • Click the Resources section
  • In the Add Resource drop down select Add Existing File
  • Select any resource file. (I picked an existing image file)
    • At this point my errors disappeared.
  • Select the resource you just added
  • Click Remove Resource (to clean up)
  • Close and save your properties

Late (very) response I know but there you go.



回答3:

Since none of the answers worked in my case, I decided to post by own answer.

Here is what I did:

  • Create class with same name (X) in the exact same namespace (Y).
  • "The namespace 'Y' already contains a definition for 'X'." error appears.
  • Delete class X.

After deleting the class, my resource with name X remained visible in the namespace.



回答4:

In my case the Resource.Designer.cs file was somehow excluded from project... Clicked on show hidden files (show all files), included it in project with a right-click and it was fixed. Phew..



回答5:

You most likely have tried to add another resource file with the same name as an already existing resource. To fix this simply delete the corresponding line in the Resources.resx file, for example you will see two lines that have the same "Data name" :

  <data name="button" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\button.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
  </data>