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

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

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

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

怎么了?


当前回答

如果命名空间是正确的,那么也有一个相同的错误,

只需关闭应用程序并重新打开。

这也许能解决你的问题

其他回答

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.

当我试图构建一个针对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>

我的方法是关闭解和删除”。解决方案路径下的Vs "文件夹。 (它会让你丢失所有的编辑器状态-打开的文件列表,窗口位置和错误缓存)

它总是有效的。

我遇到过这种情况(尽管这在很大程度上是我的错,是在我复制并粘贴了一些代码之后造成的);当命名空间在XAML和后面的代码之间不匹配时,就会发生这种情况

EG

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

后面的代码是

namespace NotDockPanel

对于那些在调试模式下没有错误,但在发布模式下有指定的错误(但项目运行正常)的人来说,这里有一些简单的尝试:

打开与出现问题的XAML .cs文件对应的XAML文件。 进行编辑——任何编辑,比如在某处添加空格 保存文件并关闭

这种方法在VS 2015中对我有效,根据其他用户的说法,在2017年和2019年也是如此