If I create a new project in Visual Studio 2010 SP1 and select "WPF Application" and tries to build the generated application, I get the error
The name 'InitializeComponent' does not exist in the current context.
I got a similar error this morning when I tried to build my current project. Yesterday, I had no problem compiling and running it.
I created a new project and got the error whenever I compiled the project. I have just sent the project to a colleague, and he has just compiled without any errors.
What is wrong?
Because of some reason after copying .xaml and it's .cs between projects the build action is sometimes changing. Please make sure build action of your .xaml is Page.
this happened with me when I accidentaly deleted the class reference from the xaml definition:
I've replaced the
first line with this:
I know this isn't the answer to the original question (because thats project builds on another machine), but the error message was the same, so maybe I'll help someone with this situation.
I encountered this while renaming a usercontrol. The way I fixed it was to comment out InitializeComponent, verify that all the names were correct (xaml and code behind), build the project, uncomment InitializeComponent, then build again. It sounds like there may be a couple causes/solutions for this issue, but this way did it for me.
I encountered this error during refactoring where I renamed some files/folders and the prexisiting *.g.cs files needed to be re-generated.
If you are using Xamarin Forms and you move a XAML file the "build action" of the file is changed. Xamarin Forms requires "build action = Embedded Resource".
Apply "build action" in Visual Studio:
Select the XAML file -> Properties -> Build Action = Embedded Resource
This happened to me because a Nuget package uninstaller blew away all the attributes on the <Application> element in App.xaml. This included the x:Class attribute, which specifies the application class name. So the partial class containing the InitializeComponent() method was never generated.
I fixed the problem by reverting App.xaml to the source-controlled copy.