The name 'InitializeComponent' does not exist in the current context
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 encountered this a couple times and keep forgetting what causes it. I ran into this when I renamed the namespace on my code behind file but not in my XAML.
So check if you've done the same.
The namespace and class names need to match since they are both part of a partial class
namespace ZZZ
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
//...
}
}
<!-- XAML -->
<Window x:Class="ZZZ.MainWindow">
The Build Action for the .xaml file must also be set to "Page", when moving a xaml file between projects this setting gets lost (in VS 2010 at least).
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:
- Open the XAML file corresponding to the offending xaml.cs file.
- Make an edit--any edit, like add a space somewhere
- Save the file and close it
This method worked for me in VS 2015, and according to other users, also 2017 and 2019