如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
在这个线程中,MCVE的最佳射击是,使用VS2017 15.5.2,加载LabelControlAdvancedSample的XAML,这是本教程页面的最后一个示例。
<Window x:Class="WpfTutorialSamples.Basic_controls.LabelControlAdvancedSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LabelControlAdvancedSample" Height="180" Width="250">
<StackPanel Margin="10">
<Label Target="{Binding ElementName=txtName}">
<StackPanel Orientation="Horizontal">
<Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_green.png" />
<AccessText Text="_Name:" />
</StackPanel>
</Label>
<TextBox Name="txtName" />
<Label Target="{Binding ElementName=txtMail}">
<StackPanel Orientation="Horizontal">
<Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_blue.png" />
<AccessText Text="_Mail:" />
</StackPanel>
</Label>
<TextBox Name="txtMail" />
</StackPanel>
在将App.xaml和App.xaml.cs设置为默认状态后,尝试编译上面的代码会产生链接器错误。 幸运的是,当鼠标悬停在LabelControlAdvancedSample.xaml.cs中的InitializeComponent()语句上时,会有一个链接文本提示:
显示可能的修复。
点击它会调用另一个链接文本:
生成方法MainWindow.InitializeComponent。
这样做会产生以下“do nothing”方法:
private void InitializeComponent()
{
throw new NotImplementedException();
}
必须为要构建的项目定义函数。看起来在WPF和VB.Net中InitializeComponent的实现有一些不同。 编辑:xaml第一行中的namespace.class不正确。 根据MSDN和@Sean B的回答,应该是
<Window x:Class="LabelControlAdvancedSample.MainWindow"
因此,项目编译没有错误,并且不需要虚拟的InitializeComponent方法,实际上它会产生更多的错误。去显示VS是有帮助的,即使在极其罕见的用户错误的情况下。: P
其他回答
导航到解决方案目录 删除\obj文件夹 重新构建解决方案
我在重构过程中遇到了这个错误,我重命名了一些文件/文件夹,并且需要重新生成预先存在的*.g.cs文件。
这发生在我身上,因为一个Nuget包卸载程序吹走了App.xaml中<Application>元素上的所有属性。这包括x:Class属性,它指定应用程序类名。因此从未生成包含InitializeComponent()方法的分部类。
我通过将App.xaml恢复到源控制副本来解决这个问题。
导致这个错误的另一个常见原因是:
Right click on folder in project to create new UserControl. This creates a class and xaml file that derives from user control in the namespace of the folder. Then you decide to change the namespace of the class because you're really just using folders for organization of code. The x:Class attribute will not get automatically updated so it will be searching for a class that doesn't exist. Could probably use a better error message like "x:Class type could not be found in namesace bla.blaa.blaaa."
这个问题发生在我创建一个“WPF应用程序项目”,然后将其构建目标更改为“类库”,以供另一个程序用作外部工具时。
我改变了我所有的。xaml文件为我的窗口,使他们的构建动作设置为“页”。我没有意识到的是,该项目还包含“App.xaml”和“App.xaml.cs”。
“App.xaml”也需要设置为“Page”,或者完全删除(连同“App.xaml.cs”)。我选择了前者,当我意识到这些文件毫无用处时,我选择了后者。
当我不小心从xaml定义中删除类引用时,这发生在我身上:
我已经替换了
<Window x:Class="myapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
第一行是:
<RibbonWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
我知道这不是最初问题的答案(因为这是在另一台机器上构建的项目),但错误消息是一样的,所以也许我会帮助别人解决这种情况。