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:49

Because of some reason after copying .xaml and it's .cs between projects the build action is sometimes changing. Please make sure build action of your .xaml is Page.

查看更多
冷血范
3楼-- · 2020-01-24 10:54

this happened with me when I accidentaly deleted the class reference from the xaml definition:

I've replaced the

<Window x:Class="myapp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

first line with this:

<RibbonWindow 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

I know this isn't the answer to the original question (because thats project builds on another machine), but the error message was the same, so maybe I'll help someone with this situation.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-01-24 10:54

I encountered this while renaming a usercontrol. The way I fixed it was to comment out InitializeComponent, verify that all the names were correct (xaml and code behind), build the project, uncomment InitializeComponent, then build again. It sounds like there may be a couple causes/solutions for this issue, but this way did it for me.

查看更多
贪生不怕死
5楼-- · 2020-01-24 10:56
  1. Navigate to the solution directory
  2. Delete the \obj folder
  3. Rebuild the solution

I encountered this error during refactoring where I renamed some files/folders and the prexisiting *.g.cs files needed to be re-generated.

查看更多
够拽才男人
6楼-- · 2020-01-24 10:56

If you are using Xamarin Forms and you move a XAML file the "build action" of the file is changed. Xamarin Forms requires "build action = Embedded Resource".

Apply "build action" in Visual Studio:

Select the XAML file -> Properties -> Build Action = Embedded Resource

查看更多
Anthone
7楼-- · 2020-01-24 10:56

This happened to me because a Nuget package uninstaller blew away all the attributes on the <Application> element in App.xaml. This included the x:Class attribute, which specifies the application class name. So the partial class containing the InitializeComponent() method was never generated.

I fixed the problem by reverting App.xaml to the source-controlled copy.

查看更多
登录 后发表回答