如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
其他选项:
在我使用Xamarin Forms的情况下,在“InitializeComponent”的顶部,在我的App.xaml文件中的当前上下文错误中不存在,我还注意到在输出中,.csproj文件未能构建。奇怪。
除了下面两个ItemGroup条目外,文件本身没有什么特别引人注目的:
<ItemGroup>
<EmbeddedResource Remove="App.xaml" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml" />
</ItemGroup>
我从.csproj文件中删除了这两个文件,执行了清理和重建,错误最终消失了。解决这个问题很痛苦,也许这将帮助其他人避免一些压力。
其他回答
这为我解决了问题。
我已经注释掉了App.xaml文件中的资源
<Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<!--<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>-->
</Application.Resources>
</Application>
将此注释回来以修复构建错误。
<Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
深入挖掘一下,我发现{Project}\obj\debug中的app.g.cs文件在我留下资源注释时只包含以下内容。
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/MyApp;component/app.xaml", System.UriKind.Relative);
#line 1 "..\..\..\App.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
这有一个非常具体的原因,它在项目设置中。当您试图将WPF控件/窗口添加到. net 2.0类库或项目中时,通常会发生这种情况。出现此错误的原因是项目不知道它正在构建WPF控件或窗口,因此试图将其作为c# 2.0项目构建。
解决方案涉及编辑.csproj文件。右键单击引起问题的项目,选择“卸载项目”。右键单击未加载的项目并选择“Edit .csproj”。将打开.csproj文件,您可以看到XML。看看下面这行:
<Import Project=…..
它在文件的末尾,你仅有的一行大概是
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
这告诉Visual Studio将项目构建为. net 2.0项目。我们要做的是告诉Visual Studio这实际上是一个WPF项目,所以我们必须添加以下一行:
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
这一行将告诉Visual Studio将项目构建为WPF项目。现在你的.csproj文件底部应该是这样的:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
保存.csproj文件,在解决方案资源管理器中右键单击它,并选择“重新加载项目”编译,就是这样,您都完成了!
OK this.InitializeComponent();
Wrong this.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.
我遇到过这种情况(尽管这在很大程度上是我的错,是在我复制并粘贴了一些代码之后造成的);当命名空间在XAML和后面的代码之间不匹配时,就会发生这种情况
EG
<UserControl x:Class="DockPanel.TreeView" />
后面的代码是
namespace NotDockPanel