如果我在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!
这和我之前做的顺序不一样。它需要被简化。
其他回答
因为这似乎是关于缺少'InitializeComponent'的问题的去线程,我将在这里包括我的答案。
我也有这个问题,我已经尝试了我在这里和谷歌可以找到的所有其他论坛上找到的一切,但是没有人解决了我的问题。在尝试了两个小时之后,我终于发现我的设置出了什么问题。
在我们的项目中,我们使用了来自MahApps的Metro组件。给我带来麻烦的视图是从MetroWindow继承的视图,像这样:
<Controls:MetroWindow x:Class="ProjectNamespace.MyView"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
... >
现在,我将静态资源定义为
<Controls:MetroWindow.Resources>
<prop:Resources x:Key="LocalizedStrings"/>
...
</Controls:MetroWindow.Resources>
这就是我在所有其他视图中的UserControls中定义资源的方式,所以我认为这是可行的。
然而,《Controls:MetroWindow!》在那里,我绝对需要如下的资源定义:
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<prop:Resources x:Key="LocalizedStrings"/>
...
</ResourceDictionary>
</Controls:MetroWindow.Resources>
总之,我的问题是缺少<ResourceDictionary>标记。我真的不知道为什么这会产生'InitializeComponent'错误,奇怪的是,它甚至没有在我的每台机器上产生它,但这就是我如何修复它。希望这对(剩下的0.001%遇到这个问题的人)有所帮助。
我在VS2017 Xamarin中也遇到了同样的问题。形式的项目。阅读此错误链接:
https://bugzilla.xamarin.com/show_bug.cgi?id=33181#c53
要解决本例中的问题:右键单击[xaml-file-name]。xaml,选择属性,然后将构建操作更改为内容,然后返回为嵌入式资源。
这发生在我身上,因为一个Nuget包卸载程序吹走了App.xaml中<Application>元素上的所有属性。这包括x:Class属性,它指定应用程序类名。因此从未生成包含InitializeComponent()方法的分部类。
我通过将App.xaml恢复到源控制副本来解决这个问题。
我尝试了上面所有的建议。如果你太努力而没有成功,那就走更容易的路。创建一个新页面。xaml然后复制新类的代码并删除有问题的类xaml。不要花更多的时间。
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!
这和我之前做的顺序不一样。它需要被简化。