The name 'InitializeComponent' does not ex

2020-01-24 10:05发布

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?

30条回答
霸刀☆藐视天下
2楼-- · 2020-01-24 11:01

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

namespace TrainerB.MVC.Forms
{
     public partial class AboutDeveloper : ContentPage
     {
          public AboutDeveloper()
          {
               InitializeComponent();
          }
     }
}

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

Xaml file properties

Xaml Namespace Correcting 01

Xaml Namespace Correcting 02

查看更多
欢心
3楼-- · 2020-01-24 11:01

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

查看更多
beautiful°
4楼-- · 2020-01-24 11:01

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.

查看更多
在下西门庆
5楼-- · 2020-01-24 11:02

Make sure in the xaml file

<Page x:Class="Project1.Page1" ...

match the 'Project1' name and the 'Page1' name

查看更多
爷、活的狠高调
6楼-- · 2020-01-24 11:03

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.

查看更多
beautiful°
7楼-- · 2020-01-24 11:03

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:

<Controls:MetroWindow x:Class="ProjectNamespace.MyView"
                      xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                      ... >

Now, I have defined my static resources as

<Controls:MetroWindow.Resources>
    <prop:Resources x:Key="LocalizedStrings"/>
    ...
</Controls:MetroWindow.Resources>

That's how I've defined Resources in UserControls 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:

<Controls:MetroWindow.Resources>
    <ResourceDictionary>
        <prop:Resources x:Key="LocalizedStrings"/>
        ...
    </ResourceDictionary>
</Controls:MetroWindow.Resources>

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).

查看更多
登录 后发表回答