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?
After some action the namespace of the .cs file and the one in .xaml file may be different (in xaml look for the x:Class="namespace.yourType").
Fix them to be the same.
I have discovered that the "Startup object" was (Not set) causing this error for me.
"Startup object" (Not set)
I try all suggestions above. If you try too without success get the more easy way. Create a new page.xaml then copy your code for new class and delete class XAML with problems. Don't spend more time.
For those who have no errors in Debug mode, but do have the specified error in Release mode (and yet the project runs fine), here is something simple to try:
This method worked for me in VS 2015, and according to other users, also 2017 and 2019
Another solution to this problem is to simply change the property-> Build Action on the XAML from Embedded Resource to anything else, save, then change it right back to Embedded Resource. The error goes away.
I agree with the answer above that the namespaces have to match. However, I had a problem like this where the namespaces matched.
To fix, I simply changed the namespace in the XAML to an INCORRECT one, saved, then changed it back to the CORRECT one. Voila!