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条回答
Juvenile、少年°
2楼-- · 2020-01-24 10:39

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.

查看更多
老娘就宠你
3楼-- · 2020-01-24 10:39

I have discovered that the "Startup object" was (Not set) causing this error for me.

"Startup object" (Not set)

查看更多
别忘想泡老子
4楼-- · 2020-01-24 10:41

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.

查看更多
淡お忘
5楼-- · 2020-01-24 10:46

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:

  1. Open the XAML file corresponding to the offending xaml.cs file.
  2. Make an edit--any edit, like add a space somewhere
  3. Save the file and close it

This method worked for me in VS 2015, and according to other users, also 2017 and 2019

查看更多
倾城 Initia
6楼-- · 2020-01-24 10:46

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.

查看更多
聊天终结者
7楼-- · 2020-01-24 10:48

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!

查看更多
登录 后发表回答