如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误

名称“InitializeComponent”在当前上下文中不存在。

今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。

我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。

怎么了?


当前回答

我在VS2017 Xamarin中也遇到了同样的问题。形式的项目。阅读此错误链接:

https://bugzilla.xamarin.com/show_bug.cgi?id=33181#c53

要解决本例中的问题:右键单击[xaml-file-name]。xaml,选择属性,然后将构建操作更改为内容,然后返回为嵌入式资源。

其他回答

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.

在我的情况下,程序实例已经在后台运行。我只是停止运行实例和程序构建成功。

这发生在我身上,因为一个Nuget包卸载程序吹走了App.xaml中<Application>元素上的所有属性。这包括x:Class属性,它指定应用程序类名。因此从未生成包含InitializeComponent()方法的分部类。

我通过将App.xaml恢复到源控制副本来解决这个问题。

(老问题了,但如果搜索把你带到了这里)

01/09/2022 -在visual studio社区2022经历了问题。

报告的问题是一个未更改的.xaml.cs文件。

今天对我有效的解决方法是:

复制了我一直在做的最后一个xaml文件(和xaml.cs)到一个不在项目树中的文件夹。

在解决方案资源管理器中右键单击文件并选择“删除” (这将删除文件)

重新构建项目(在引用文件的地方会出现错误)

将xaml和xaml.cs文件复制回项目文件夹

右键单击项目,选择“添加/现有项目”,并选择xaml文件。 重新构建项目

(对我来说,它构建和其他xaml文件上的错误已经消失了)

给那些在网上找到这个的人。检查窗户。Csproj文件,如果那里有编译。应该有2项

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

<Compile Include="YourFile.xaml.cs">
  <DependentUpon>YourFile.xaml</DependentUpon>
</Compile>