如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误

名称“InitializeComponent”在当前上下文中不存在。

今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。

我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。

怎么了?


当前回答

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!

这和我之前做的顺序不一样。它需要被简化。

其他回答

另一种可能的解释是,您正在针对x86进行构建。右键单击解决方案并选择配置管理器。看看你是否在x86而不是任何CPU上构建。

在我的情况下,我的项目中有一个错误的引用,但没有报告。

我通过阅读我所有的推荐信来解决我的问题,即使它们是正确的。

问题出现时,我没有改变代码,使用最近的VS2019 16.7.5。

只要打开相应的XAML文件就可以解决这个问题。

当我不小心从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">

我知道这不是最初问题的答案(因为这是在另一台机器上构建的项目),但错误消息是一样的,所以也许我会帮助别人解决这种情况。

导航到解决方案目录 删除\obj文件夹 重新构建解决方案

我在重构过程中遇到了这个错误,我重命名了一些文件/文件夹,并且需要重新生成预先存在的*.g.cs文件。