如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
我有同样的问题,期望我把我的主窗口xaml和cs复制到一个新文件,然后把它们复制回原来的地方。在尝试编译WPF应用程序后,我得到了这个错误。
我修复这个错误的方法是重命名命名空间(from egNamespace -> egNamespaceNew),它再次工作。然后我将名称空间改回原来的名称空间。
其他回答
检查设计器文件。
我也有同样的问题。在我的例子中,原因是FileName.Designer.cs的名称空间与FileName.cs中使用的(正确的)名称空间不匹配。
更改FileName.Designer.cs的命名空间以匹配FileName.cs立即解决了这个问题。
上面的答案对我都没用。除了那些重复的我都试过了。然而,由于一些奇怪的原因,这在我的Visual Studio 2015的跨平台项目中起作用:
在“解决方案资源管理器”中右键单击导致问题的项目。在弹出菜单中选择:Add——> Class 选择跨平台——> Forms Xaml Page。保持漂亮的Page1.cs标准名称并单击Add。 注意前面的InitializeComponent()问题是如何由于某种原因消失的。 删除新创建的Page1.cs并继续编程,就像Visual Studio工作正常一样。
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!
这和我之前做的顺序不一样。它需要被简化。
导航到解决方案目录 删除\obj文件夹 重新构建解决方案
我在重构过程中遇到了这个错误,我重命名了一些文件/文件夹,并且需要重新生成预先存在的*.g.cs文件。
导致这个错误的另一个常见原因是:
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."