如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
这为我解决了问题。
我已经注释掉了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
}
其他回答
当您从另一个项目导入类,或更改xaml文件的路径,或xaml或.cs文件后面的名称空间时,可能会出现此错误。
第一:它的名称空间可能与新项目中的名称空间不相同
namespace TrainerB.MVC.Forms
{
public partial class AboutDeveloper : ContentPage
{
public AboutDeveloper()
{
InitializeComponent();
}
}
}
正如您所看到的,导入文件中的名称空间以旧的项目名称开始:“TrainerB”,但您的新项目可能有不同的名称,所以只需在.xaml文件和后面的.cs文件中将其更改为正确的新项目名称。
Two:
将.xaml文件的属性更改为:
构建动作:嵌入式资源
自定义工具:MSBuild:UpdateDesignTimeXaml
帮助我的是将.csproj中的第一行更改为
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
我也有同样的问题,但对我来说,这些都没用。在我的情况下,我拥有的每个WPF项目(包括新创建的项目)都停止编译此错误。最后,我卸载了所有的。net框架,然后重新安装了它们,一切又开始工作了。我还重新安装了Visual Studio,但结果没有影响。
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.
右键单击项目中的文件夹以创建新的UserControl,这是我的问题。 我在文件夹外创建了相同的控件,就是这样。