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?
You might get this error when you import a class from another project, or change the path of the xaml file, or the namespace of either the xaml or behind .cs file.
One: It might have a namespace that is not the same as what you have in you new project
As you can see the name space in the imported file begins with the old project name: "TrainerB", but your new project might have a different name, so just change it to the correct new project name, in both the .xaml file and the behind .cs file.
Two:
change the properties of the .xaml file to:
Build Action: Embedded Resource
Custom Tool: MSBuild:UpdateDesignTimeXaml
If the Namespaces are correct then also there is a same error,
Just close your application and open it again.
This may solve your problem
Another possible explanation is that you're building against x86. Right-click your Solution and choose Configuration Manager. See if you're building against x86 instead of Any CPU.
Make sure in the xaml file
match the 'Project1' name and the 'Page1' name
Check the Designer file.
I had this same issue. In my case, the cause was that the
namespace
for FileName.Designer.cs did not match the (correct)namespace
used in FileName.cs.Changing the
namespace
of FileName.Designer.cs to match that of FileName.cs solved the problem immediately.Since this seems to be the go-to thread for the problem regarding missing 'InitializeComponent', I'll include my answer here.
I too was having this issue and I've tried everything I found here and in all other Forums that Google could find, however none resolved the issue for me. After two hours of trying everything, I finally figured out what was wrong with my setup.
In our project, we are using Metro components from MahApps. The view that was giving me trouble was a view inheriting from MetroWindow, like this:
Now, I have defined my static resources as
That's how I've defined Resources in
UserControl
s in all my other views, so that's what I assumed will work.That was, however, not the case with
Controls:MetroWindow
! There I absolutely needed the resource definition as follows:So my issue, in summary, was a missing
<ResourceDictionary>
tag. I really don't know why this produced the 'InitializeComponent' error and it weirdly didn't even produce it on every machine of mine, but that's how I fixed it. Hope this helps (the remaining 0.001% of people encountering this issue).