The name 'InitializeComponent' does not exist in the current context(c#,xamarin) - c#

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.

Related

The name "X" does not exist in the namespace "using:Y"

After reading every article here and trying every suggested action I'm out of things to try:
I have a namespace issue in Visual Studio 2015 - in a XAML file in UWP.
The concrete error message is:
"The name "FieldManagementViewModel" does not exist in the namespace
"using:StudyConfigurationClient.ViewModels"".
This is my XAML:
This is the structure to show, that the folder and file actually exists, folder structure equals namespaces in this project:
I've looked all references and namespaces through, I've repaired Visual Studio 2015, I've cleaned and rebuilt several times and changed platform for the build back and forth - I'm out of options.
What worked for me:
Remove the problematic XAML tag, so that solution will build
Rebuild project
Restore the tag
I think VS falls into a chicken & egg situation here, in that it needs newly added classes built so that XAML can see them, but it can't build them because XAML can't see them.
Just add this solution to this probelm:
- Close and re-open Visual Studio
In my case the problem occurred in App.xaml. In the properties for App.xaml the BuildAction had somehow been changed to Page. After restoring the BuildAction to ApplicationDefinition the problem went away. (See also this related answer.)
Thanks for helping - this issue magically went away when I pulled a change from git, which shouldn't affect this in any way. Sorry for not being able to help others, but I haven't got a clue about what happened.

"No Code Behind File"

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.

Can't see Resources namespaces in ASP.NET MVC application

Everything was fine but suddenly Resources stopped working.
I have ASP.NET MVC5 Application.
All i see is message:
Error 3: The type or namespace name 'User' does not exist
in the namespace 'MyProject.Resources' (are you missing
an assembly reference?)
But, this problem is only in .cs files! In Views everything is OK!
All I see for example in some controller is Resources.App_LocalResources but this is empty namespace. In .cshtml file I see all my resources!
It looks like some problem with Visual Studio, because it happend suddenly. Earlier everything was fine.
Any ideas?
I have already restarted Visual Studio many times, I have resterd even computer. But nothing's changed.
EDIT:
Problem solved.
I have replaced .csproj of my Resources project to older version. I had to add again few files but now everything's OK.
So it was damaged project file.
You can do "unload project" when right clicking on the Core/database project. Right click on the project file again selecting "edit". Now you should be able to manually edit the csproj file.
Try adding the missing references, or editing them if they are pointing to the output folder of your build
I had a similar issue and I solved it doing the following steps:
Changing the Resource Propertie 'Custom Tool' to 'PublicResXFileCodeGenerator'.
After changing this propertie, the Resource Class was automaticaly updated as follows: Class Updated.
After these steps I was able to make the correct reference to the resource file.
Hope it helps!
I can't believe that!
Project is crashing after adding exactly 10 resources files!
As long as there is max 9 files - it works. I add 10th file and errors appear! I remove this file, and everything is OK again!
How can this happen?!

Resource.designer.cs NOT regenerating in Mono (Xamarin Studio)

I'm currently developing an app with Mono, I recently updated to Xamarin Studio 4.0.8, and was having problems with the design elements not being added to the solution.
I tried to edit manually the designer.cs, and later on I erased it, hoping the program will regenerate it again, with the layout changes.
I have tried:
Rebuilding it
Cleaning and rebuilding
Erasing the file through the file explorer
And it still doesn't regenerate properly, Xamarin creates the Resource.designer.cs but it's empty, and when compiling it returns this:
Error CS0103: The name 'Resource' doesn't exist in the current context (CS0103)
Help please...
Lots of time having this issue. What i did:
Remove "resources.designer.cs" using Xamarin (secondary button->remove).
Go to command line (finder on Mac/Explorer on Windows) and delete manually the file (be sure you deleted it).
Go to Xamarin, clean->build (this will regenerate the resoruces.designer.cs).
As you remove the file from the project, you have to add it again. secondary button over project-> add Files->select Resources.designer.cs (tipically on /Resources folder).
Make sure the "Build Action" of the Resource.designer.cs file is Compile (sometimes it takes a different build action by default).
Clean and build.
At least it have worked most of the time.
I had this happen a while back. If you right click on your XML file, and click properties. Make sure that the Build Action is set to "AndroidResource"
If I recall correctly that fixed it
This work for me:
Open your .designer.cs file and erase all class content, letting only the class declaration empty.
open .xib file or storyboard that give you trouble with your .cs file.
update Identify>Class property of the view that use that .designer.cs class removing, unfocus, focus and setting again the property value.
save
rebuild

What could cause Visual Studio / C# error MSB3105: Duplicate resources

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.

Categories