如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在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!
这和我之前做的顺序不一样。它需要被简化。
其他回答
上面的答案对我都没用。除了那些重复的我都试过了。然而,由于一些奇怪的原因,这在我的Visual Studio 2015的跨平台项目中起作用:
在“解决方案资源管理器”中右键单击导致问题的项目。在弹出菜单中选择:Add——> Class 选择跨平台——> Forms Xaml Page。保持漂亮的Page1.cs标准名称并单击Add。 注意前面的InitializeComponent()问题是如何由于某种原因消失的。 删除新创建的Page1.cs并继续编程,就像Visual Studio工作正常一样。
对于那些在调试模式下没有错误,但在发布模式下有指定的错误(但项目运行正常)的人来说,这里有一些简单的尝试:
打开与出现问题的XAML .cs文件对应的XAML文件。 进行编辑——任何编辑,比如在某处添加空格 保存文件并关闭
这种方法在VS 2015中对我有效,根据其他用户的说法,在2017年和2019年也是如此
此问题的另一个解决方案是简单地将属性-> Build Action从嵌入式资源更改为其他任何内容,保存,然后将其更改为嵌入式资源。错误消失了。
这个问题发生在我创建一个“WPF应用程序项目”,然后将其构建目标更改为“类库”,以供另一个程序用作外部工具时。
我改变了我所有的。xaml文件为我的窗口,使他们的构建动作设置为“页”。我没有意识到的是,该项目还包含“App.xaml”和“App.xaml.cs”。
“App.xaml”也需要设置为“Page”,或者完全删除(连同“App.xaml.cs”)。我选择了前者,当我意识到这些文件毫无用处时,我选择了后者。
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!
这和我之前做的顺序不一样。它需要被简化。