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

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

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

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

怎么了?


当前回答

在我的情况下,我的项目中有一个错误的引用,但没有报告。

我通过阅读我所有的推荐信来解决我的问题,即使它们是正确的。

其他回答

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

EG

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

后面的代码是

namespace NotDockPanel

我遇到过几次这种情况,但总是忘记是什么原因造成的。 当我在文件后面的代码上重命名命名空间时,我遇到了这个问题,但不是在XAML中。

所以检查一下你是否做了同样的事情。

名称空间和类名需要匹配,因为它们都是部分类的一部分

namespace ZZZ
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
         //...
    }
}

<!-- XAML -->
<Window x:Class="ZZZ.MainWindow">

我刚刚遇到了这个问题,原来是我的项目存储在我的用户文件夹中,它存储在网络上,我们有一个短暂的网络中断。我做了一个模型;它抱怨说,我的文件在编辑器之外被修改了(其实没有;文件锁刚刚被borked),它构建得很好,删除了关于InitializeComponent()方法的错误。

顺便说一句,如果你想知道,从网络驱动器开发一些东西是不好的做法。当你试图利用。net的托管代码时,这就变得特别成问题;根据我的经验,每次你建造的时候它都会崩溃。我忘了把这个一次性的小项目放在合适的文件夹里,最终付出了代价。

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

另一种可能的解释是,您正在针对x86进行构建。右键单击解决方案并选择配置管理器。看看你是否在x86而不是任何CPU上构建。