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?
I've had this (although it was very much my fault and was caused after I copied and pasted some code in); it can occur when the namespace doesn't match between the XAML and code behind
EG
and the code behind is
None of the above answers worked for me. I tried them all except the duplicate ones. However for some weird reason this worked in my cross-platform project in Visual Studio 2015:
Unload the entire solution and then reload it again. Then Rebuild the solution. This resolved the issue for me.
Another common cause of this error is if you did something in this:
Right click on folder in project to create new UserControl. This creates a class and xaml file that derives from user control in the namespace of the folder.
Then you decide to change the namespace of the class because you're really just using folders for organization of code. The x:Class attribute will not get automatically updated so it will be searching for a class that doesn't exist. Could probably use a better error message like "x:Class type could not be found in namesace bla.blaa.blaaa."
I know this was answered due to a different cause, but this is a highly hit posting and I had ran into the same issue with a class library. In this case, it turned out to be both a change in my namespace (answered in this post here) and that the compiler could not rebuild the Window.g.i.cs which defines the InitializeComponent() method. It couldn't because the class library was missing the ProjectTypeGuid value for WPF projects in the csproj file. Instructions for this are here and here. I thought I would share in case someone else has run into the same issue. Just changing the namespace isn't enough in this case.
For those who find this on the internet. Check the Windows.csproj file if the compilation is there. There should be 2 entries