如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
我在使用迁移向导将。net461项目迁移到新的sdk风格的csproj格式时遇到了这个问题。我必须把这个添加到我的csproj:
<PropertyGroup>
<UseWPF>true</UseWPF>
然后删除从迁移向导生成的任何Page标记……
<Page Include="Views\MyCustomControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
其他回答
当我试图构建一个针对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>
帮助我的是将.csproj中的第一行更改为
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
我也有同样的问题,但对我来说,这些都没用。在我的情况下,我拥有的每个WPF项目(包括新创建的项目)都停止编译此错误。最后,我卸载了所有的。net框架,然后重新安装了它们,一切又开始工作了。我还重新安装了Visual Studio,但结果没有影响。
我知道这是一个老问题了,但我们也遇到过类似的问题。我们能够使用VS2012构建一个项目,但不能从命令行使用msbuild。我进入。proj文件,并注意到它在默认的“PropertyGroup”部分下没有“ProjectTypeGuids”的记录,所以我添加了这个:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
这是WPF的项目GUID。然后我删除并重新添加UserControl,它开始工作。我不确定我是否必须做最后一步,但现在它对我有用。
另一种可能的解释是,您正在针对x86进行构建。右键单击解决方案并选择配置管理器。看看你是否在x86而不是任何CPU上构建。