While working on an existing project I suddenly got the following error when trying to compile the solution:
error MSB3105: The item "[filename]" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.
Now, as far as I'm aware, I did not make any change to the project that affects the resources. Also I have checked each and every file within the project, but there is no duplicate reference anywhere to this file.
Now I already found some forum entries regarding this error:
1) Open the .csproj file and remove the duplicate reference. [Tried this, but I cannot find any duplicates in it]
2) In a 'partial class' project, move everything to a single class. [ Could try this, but the project has been split up into partial classes since the start, and I do not want to change this just because of the error ]
So what else could cause this ?
Did you try showing all files in the Solution Explorer? You could have a duplicate .rsx file somewhere in there.
I found the answer in .NET forum posting by Roy Green, and Theresa was right after all, though I did not recognize it.
If you have your main form class split up into partial classes, the partial sections end up in the solution explorer as separate items. And if you double click on them they show up in the designer mode as a normal form. But if you (accidentally) drop a control on these forms, Visual Studio creates a new .resx file and a InitializeComponent routine for it. But since this form is actually just part of the Main Form class it leads to the 'duplicate resources' error. And there is no other solution but to remove the InitializeComponent routine and delete the .resx file by hand.
Be sure that under yourForm.cs no duplicate resources are defined (.resx). If you renamed your Form, remove the old resource because the new one during compile will be created with the new name.
I just made the same mistake. Delete the mainform.designer.vb, then I restored it again from the recycle bin, and found this error message when compiling.
I try to search on google and someone suggested to check on .vbproj. Did that and found a duplicate on some line.
I had this as well, in VB. There is the "real form" file frmMain, and then I had created new class files and modified them to be Partial Public Class frmMain. For example, I have an ImportFromExcel.vb Partial Class file (I didn't want to clutter up the frmMain.vb with the rather complicated Excel import code.)
Everything worked fine until I decided I wanted to use an OpenFileDialog in the Sub ImportFromExcel. I dragged the OFD from the toolbox over to the Designer view of the ImportFromExcel file. (I have no idea why this view exists, if you can't do anything with it!) But at any rate... dragging the OFD to the Partial Class Designer created an ImportFromExcel.resx file. The drag/drop operation also created an InitializeComponent sub in ImportFromExcel, which is redundant and shows an error -- easily corrected with a little editing.
Ultimately, I chose to not use the dragged resource, but localized the code in the ImportFromExcel.vb file.
All you really have to do is right-click the ImportFromExcel.resx file, and choose Delete. Everything else seems to "fix itself", and it builds fine now.
In my case, this problem happened because a file had the same name but not the same case in the GIT repository.
For example MyFile.cs and myFile.cs.
If you do a checkout on windows, one of the files is overwritten by the other (no message, no warning). So, it is compiling, and we don't notice anything. But if you try to compile on Linux (with .NET Core) both files are present, and there is this error at compile time.
Related
I just downloaded Visual Studio and created a new Xamarin.Forms project. However, when building the app with the auto-generated code I got stuck on the following error in the code-behind of MainPage.xaml:
The name 'InitializeComponent' does not exist in the current context
I looked up solutions on the internet but did not find one that works for me.
What I tried:
Changing the custom tool in the properties of MainPage.xaml (which was empty beforehand)
I checked if the namespaces match (they do)
I tried saving all the files
Here is an image of the auto-generated code, which I didn't edit:
If you are having successful builds, but a nagging initializecomponent does not exist error,
CLOSE VS,
DELETE the hidden .vs folder in your project (this clears intellisense).
OPEN VS - the error is gone.
I've had this issue crop up here and there where InitializeComponent and a number of other classes and methods "does not exist in the current context". Not sure of the underlying problem, but if it is the same as I've had, I unload and reload all projects. This can be accomplished by right clicking on the project in the solution explorer, clicking unload, right clicking again, and hitting reload.
I had also this problem sometimes when creating a new page, make sure that the BuildAction of the page is set to "Embedded resource" as other normal pages
This is what I did to remove the error on my Xamarin project. I closed the project and removed obj folders. Then reloaded the Project again and the error disappeared.
Check the ContentPage property of your XAML file. Make sure that the x:Class value, is correct, example: x:Class="YourProjectName.Views.ContentPageName">
I created a ContentPage and then renamed it, but the x:Class wasn't updated, so I had to do it manually. Tried first unload/reload but didn't work in my case.
In one case also, I created a content page with the name 'Buttons' and had the same issue. I deleted it and created another one with the name 'UsingButtons' and was ok. Maybe there are reserved words for file names.
I recently picked up a project of mine from a few months ago that I had stored on Github. However, there are a number of forms that apparently did not get stored in the repo. I'm not sure how that happened, but I'm trying to recover as much as I can.
For example:
- CreditsBox.cs
- CreditsBox.Designer.cs
In this case, the CreditsBox.Designer.cs file exists, but the CreditsBox class file does not. Fortunately, the forms that this happened to didn't have too much login in them, so its trivial to rewrite. However, it would make it 10x easier if I could somehow rebuild the form with just the designer file?
Any help is appreciated!
The designer file is all you need to re-create the UI of your forms.
You could follow these steps
Create a new Project.
Add a form and name its file as CreditBox.cs.
Use your saved designer file to replace the one created by Visual
Studio.
As far as I know, the last step could be done also with Visual Studio open but, in any case, better close VS and reopen it afterward.
Of course you could do the same for all other form designer files with the missing main cs file.
So I found a somewhat easy way to fix this.
Create a backup of the .Designer.cs file and delete the original from the project. Then, create a new form in your project with the original form's name (so dependent methods/calls don't fail). Then go through your original .Designer.cs file and create the objects simply by dragging from the Toolbox onto the form and naming it to match the original file. Once you have all of them added (don't worry about styling), copy the old Designer.cs content back in so it will fix the styling, spacing, and all that. Once you build, the designer will update to the original styling/spacing/etc.
I have been working on c# Gui for about a week without problem, all of a sudden when I added a label control I tried to access its events and saw the error "This document item has no code behind file. Add code behind file and a class definition before adding event handlers"
This is odd since the gui/methods still runs fine but it is making it very difficult to progress...any ideas? Did a file get deleted etc? Can provide more info on request, thanks for the help!
(couldn't add screen shot unfortunately yet but error is displayed in properties window/event tab)
I encountered the same error"This document item has no code behind file. Add code behind file and a class definition before adding event handlers" in my first C++ Helloworld project when I try to add a even handler for one button I added in the MainPage.xaml.
I solved the problem by restart Visual Studio 2015 and recreate a new project, then I add a even handler for the button successfully.
It's my experience and just be posted here that maybe you can use it as reference.
I ran into this, too. In my project, most UserControls were fine, but there was one group that showed the "no code-behind" message instead of properties. The .cs files did exist, though. In the Solution Explorer tree, the xaml files all had xaml.cs files. I checked the csproj file, and all the xaml.cs files were tagged as <DependentUpon> the correct matching xaml file.
I was able to fix this by closing the solution, then deleting the csproj.vspscc (Source Control Project Metadata) file for the project. When I reopened the solution, that metadata file was rebuilt and the Properties were available in Visual Studio again.
I created a new class library for windows store apps and within this library I created a Usercontrol in a folder named 'Controls'.
I use this class library in another project (especially this controls mentioned above) and everytime I try to run this project a weird error occurs:
"Payload file 'C:\PATH TO CLASSLIBRARY FOLDER\bin\Release\LIBRARYNAME\Controls\Usercontrol.xaml' could not be found"
So I opened my library's folder and looked up this path.
The path described in the error message does not exists but if you would replace
\bin\Release\LIBRARYNAME\Controls\Usercontrol.xaml with \bin\Release\Controls\Usercontrol.xaml then there is the wanted file.
Then I simply copied the \Controls\ folder containing Usercontrol.xaml and pasted it into the newly created folder \LIBRARYNAME\Controls\Usercontrol.xaml.
Now this error doesn't occur anymore but I don't want to do this copy-paste action everytime I modify Usercontrol.xaml?!
Does anybody know why this happens? Is there a way to avoid my inconvenient solution?
Thanks for help! :)
After doing some further researching with the exacty english error message (worked with german vs2012 by then) it seems that this is a bug in Visual Studio.
More info about this:
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/51111470-8a86-44d4-acb8-e268afa7564e/
The item obj\Debug\BookStore.MainForm.resources was specified more than once in the Resources parameter. Duplicate items are not supported by the Resources parameter.
How do I fix this?
This error happened when you copy and paste a form in the 'Solution Explorer' pane. Then you change name of one forms. Visual studio give the same name for both and then, when you rename form, Visual Studio do some refactoring for you and rename Designer class for both forms (not only for new).
You can fix this two ways:
Close Microsoft Visual Studio, then browse to your project folder from external program (ex. Total Commander). Into the folder where both forms are defined you can see your form's .cs and .Designer.cs file. Open the .cs and .Designer.cs files for your form in a text editor. Find and replace every instance of the original form name with new name. And if you rename already rename some resources, check file for original form also (if they are only original form name but not anything for new form name). Then save all files, start Microsoft Visual Studio and rebuild your project.
You can try chcek and rename declaration for each occurance of original form name in new .cs and .Designer.cs files but try disable refactoring. If you already renamed new form, in .Designer.cs for old form you maybe see this:
namespace YourProjectNamespace
{
partial class NewFormName
{ .. }
You must rename NewFormName to OldFormName and when Visual Studio offer you a refactoring menu (small reg rectangle before last letter of renamed object) do not make anything.
This happened to me when I created a partial form class. When I unintentionally viewed it in Design mode it created a localized resx file for the new partial form. I deleted that resx file and that resolved it.
you cant add to the resources file 2 items with the same name/key. it has to be unique
Check the *.resx files in your project as #Royi Namir described.
In case you have already done that: check your .resx.designer files. Some programs "optimize" the designer files.
The designer files are just plain xml. If they are not in sync with the corresponding xml you'll get an exception as described.
I have same problem, and I able to resolve it by these steps:
Right-Click to your form name example: MainForm
To Review: Select Find All References now you will see the list of form of possible conflicts
To Fix: Select Refactor -> Rename -
Select Preview reference changes
Rename it Differently
Un-checked for the Conflict Form/class