如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
问题出现时,我没有改变代码,使用最近的VS2019 16.7.5。
只要打开相应的XAML文件就可以解决这个问题。
其他回答
导航到解决方案目录 删除\obj文件夹 重新构建解决方案
我在重构过程中遇到了这个错误,我重命名了一些文件/文件夹,并且需要重新生成预先存在的*.g.cs文件。
因为这似乎是关于缺少'InitializeComponent'的问题的去线程,我将在这里包括我的答案。
我也有这个问题,我已经尝试了我在这里和谷歌可以找到的所有其他论坛上找到的一切,但是没有人解决了我的问题。在尝试了两个小时之后,我终于发现我的设置出了什么问题。
在我们的项目中,我们使用了来自MahApps的Metro组件。给我带来麻烦的视图是从MetroWindow继承的视图,像这样:
<Controls:MetroWindow x:Class="ProjectNamespace.MyView"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
... >
现在,我将静态资源定义为
<Controls:MetroWindow.Resources>
<prop:Resources x:Key="LocalizedStrings"/>
...
</Controls:MetroWindow.Resources>
这就是我在所有其他视图中的UserControls中定义资源的方式,所以我认为这是可行的。
然而,《Controls:MetroWindow!》在那里,我绝对需要如下的资源定义:
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<prop:Resources x:Key="LocalizedStrings"/>
...
</ResourceDictionary>
</Controls:MetroWindow.Resources>
总之,我的问题是缺少<ResourceDictionary>标记。我真的不知道为什么这会产生'InitializeComponent'错误,奇怪的是,它甚至没有在我的每台机器上产生它,但这就是我如何修复它。希望这对(剩下的0.001%遇到这个问题的人)有所帮助。
我的方法是关闭解和删除”。解决方案路径下的Vs "文件夹。 (它会让你丢失所有的编辑器状态-打开的文件列表,窗口位置和错误缓存)
它总是有效的。
当我试图构建一个针对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>
我在使用迁移向导将。net461项目迁移到新的sdk风格的csproj格式时遇到了这个问题。我必须把这个添加到我的csproj:
<PropertyGroup>
<UseWPF>true</UseWPF>
然后删除从迁移向导生成的任何Page标记……
<Page Include="Views\MyCustomControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>