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

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

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

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

怎么了?


当前回答

我知道这是一个老问题了,但我们也遇到过类似的问题。我们能够使用VS2012构建一个项目,但不能从命令行使用msbuild。我进入。proj文件,并注意到它在默认的“PropertyGroup”部分下没有“ProjectTypeGuids”的记录,所以我添加了这个:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

这是WPF的项目GUID。然后我删除并重新添加UserControl,它开始工作。我不确定我是否必须做最后一步,但现在它对我有用。

其他回答

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

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

我在重命名用户控件时遇到了这个问题。我修复它的方法是注释掉InitializeComponent,验证所有名称都是正确的(xaml和后面的代码),构建项目,取消InitializeComponent的注释,然后再次构建。听起来这个问题可能有几个原因/解决方案,但这种方法对我来说很管用。

当我试图构建一个针对net48的SDK项目时,这种情况发生在我身上。它与MSBuild.exe一起工作很好,但dotnet构建失败与给定的错误。

对我有用的是

在<PropertyGroup/>节点中添加<UseWPF>true</UseWPF> 删除所有<Page />元素,因为它们现在已被自动包含。

现在msbuild和dotnet都可以构建了。

以下是完整的项目文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{07465991-AC74-4C22-B1DE-7BA67A040631}</ProjectGuid>
    <OutputType>library</OutputType>
    <RootNamespace>MyRootNamespace</RootNamespace>
    <AssemblyName>MyAssemblyName</AssemblyName>
    <FileAlignment>512</FileAlignment>
    <PackagesDirectory>$(UserProfile)\.nuget\packages</PackagesDirectory>
    <ResolveNuGetPackages>true</ResolveNuGetPackages>
    <SkipValidatePackageReferences>true</SkipValidatePackageReferences>
    <TargetFramework>net48</TargetFramework>
    <TargetFrameworkProfile />
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <IsNetCoreProject>false</IsNetCoreProject>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <AutoGenerateBindingRedirect>true</AutoGenerateBindingRedirect>
    <ResolveAssemblyReferencesSilent>true</ResolveAssemblyReferencesSilent>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <UseWPF>true</UseWPF>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>embedded</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>embedded</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
</Project>

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

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

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

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

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

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

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

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

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

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

这个问题发生在我创建一个“WPF应用程序项目”,然后将其构建目标更改为“类库”,以供另一个程序用作外部工具时。

我改变了我所有的。xaml文件为我的窗口,使他们的构建动作设置为“页”。我没有意识到的是,该项目还包含“App.xaml”和“App.xaml.cs”。

“App.xaml”也需要设置为“Page”,或者完全删除(连同“App.xaml.cs”)。我选择了前者,当我意识到这些文件毫无用处时,我选择了后者。