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 10:37

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

<UserControl x:Class="DockPanel.TreeView" />

and the code behind is

namespace NotDockPanel
查看更多
走好不送
3楼-- · 2020-01-24 10:38

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:

  1. Right-click the project that is causing the problem in the Solution Explorer. In the pop-up menu choose: Add --> Class
  2. Select cross-platform --> Forms Xaml Page. Keep the pretty Page1.cs standard name and click Add.
  3. Notice how the previous InitializeComponent()-problem just disappeared for some reason.
  4. Delete the newly created Page1.cs and continue programming as if Visual Studio was working just fine.
查看更多
小情绪 Triste *
4楼-- · 2020-01-24 10:38

Unload the entire solution and then reload it again. Then Rebuild the solution. This resolved the issue for me.

查看更多
来,给爷笑一个
5楼-- · 2020-01-24 10:38

Another common cause of this error is if you did something in this:

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

  2. 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."

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-01-24 10:38

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.

查看更多
手持菜刀,她持情操
7楼-- · 2020-01-24 10:38

For those who find this on the internet. Check the Windows.csproj file if the compilation is there. There should be 2 entries

<Page Include="YourFile.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
</Page>

<Compile Include="YourFile.xaml.cs">
  <DependentUpon>YourFile.xaml</DependentUpon>
</Compile>
查看更多
登录 后发表回答