如果我在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
其他回答
我在重命名用户控件时遇到了这个问题。我修复它的方法是注释掉InitializeComponent,验证所有名称都是正确的(xaml和后面的代码),构建项目,取消InitializeComponent的注释,然后再次构建。听起来这个问题可能有几个原因/解决方案,但这种方法对我来说很管用。
(老问题了,但如果搜索把你带到了这里)
01/09/2022 -在visual studio社区2022经历了问题。
报告的问题是一个未更改的.xaml.cs文件。
今天对我有效的解决方法是:
复制了我一直在做的最后一个xaml文件(和xaml.cs)到一个不在项目树中的文件夹。
在解决方案资源管理器中右键单击文件并选择“删除” (这将删除文件)
重新构建项目(在引用文件的地方会出现错误)
将xaml和xaml.cs文件复制回项目文件夹
右键单击项目,选择“添加/现有项目”,并选择xaml文件。 重新构建项目
(对我来说,它构建和其他xaml文件上的错误已经消失了)
我有同样的问题,期望我把我的主窗口xaml和cs复制到一个新文件,然后把它们复制回原来的地方。在尝试编译WPF应用程序后,我得到了这个错误。
我修复这个错误的方法是重命名命名空间(from egNamespace -> egNamespaceNew),它再次工作。然后我将名称空间改回原来的名称空间。
在我的案例中(NET Core 3.1),我通过在项目文件的ProjectGroup部分中给它一个AssemblyName标记来修复它。如。
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<AssemblyName>ProjectNameUsually</AssemblyName>
</PropertyGroup>
...
</Project>
这也修复了编译器在代码背后看不到x:Name控件的问题。
I had a problem similar to this in WPF. I had copied a usercontrol but had forgotten to rename the public ViewModel property in its.xaml.cs file for it. But still wasn't working. I eventually deleted all of the files inside the obj\debug type of folder for the project (we had this named differently). And rebuilt it. It still wasn't working, and I exited Visual Studio. (Before this I had at one point two instances of Visual Studio I think maybe.) The problem I had was that it was reporting it had this error when I had already fixed it! When I came back into Visual Studio (after deleting the debug stuff in obj), there finally was no error indicator. Then I rebuilt the project and now all was good!
这和我之前做的顺序不一样。它需要被简化。