如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
OK this.InitializeComponent();
Wrong this.InitializeComponent;
其他回答
我尝试了上面所有的建议。如果你太努力而没有成功,那就走更容易的路。创建一个新页面。xaml然后复制新类的代码并删除有问题的类xaml。不要花更多的时间。
在我将一个新的平台“x86”添加到解决方案配置管理器后,我也有这个错误消息。之前它只有“任何CPU”。解决方案仍在运行,但在错误窗口中显示了多个此类错误消息。
I found the problem was that in the project properties the 'Output Path' was now pointing to "bin\x86\Debug". This was put in by Configuration Manager at the add operation. The output path of the 'Any CPU' platform was always just "bin" (because this test project was never built in release mode), so the Configuration Manager figured it should add the "\x86\Debug" by itself. There was no indication whatsoever from the error messages that the build output could be the cause and I still don't understand how the project could even run like this. After setting it to "bin" for all projects in the new 'x86' configuration all of the errors vanished.
上面的答案对我都没用。除了那些重复的我都试过了。然而,由于一些奇怪的原因,这在我的Visual Studio 2015的跨平台项目中起作用:
在“解决方案资源管理器”中右键单击导致问题的项目。在弹出菜单中选择:Add——> Class 选择跨平台——> Forms Xaml Page。保持漂亮的Page1.cs标准名称并单击Add。 注意前面的InitializeComponent()问题是如何由于某种原因消失的。 删除新创建的Page1.cs并继续编程,就像Visual Studio工作正常一样。
这发生在我身上,因为一个Nuget包卸载程序吹走了App.xaml中<Application>元素上的所有属性。这包括x:Class属性,它指定应用程序类名。因此从未生成包含InitializeComponent()方法的分部类。
我通过将App.xaml恢复到源控制副本来解决这个问题。
在一些操作之后,.cs文件的命名空间和。xaml文件中的命名空间可能是不同的(在xaml中查找x:Class="namespace. yourtype ")。
使它们相同。