如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误
名称“InitializeComponent”在当前上下文中不存在。
今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。
我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。
怎么了?
当前回答
其他选项:
在我使用Xamarin Forms的情况下,在“InitializeComponent”的顶部,在我的App.xaml文件中的当前上下文错误中不存在,我还注意到在输出中,.csproj文件未能构建。奇怪。
除了下面两个ItemGroup条目外,文件本身没有什么特别引人注目的:
<ItemGroup>
<EmbeddedResource Remove="App.xaml" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml" />
</ItemGroup>
我从.csproj文件中删除了这两个文件,执行了清理和重建,错误最终消失了。解决这个问题很痛苦,也许这将帮助其他人避免一些压力。
其他回答
我在使用迁移向导将。net461项目迁移到新的sdk风格的csproj格式时遇到了这个问题。我必须把这个添加到我的csproj:
<PropertyGroup>
<UseWPF>true</UseWPF>
然后删除从迁移向导生成的任何Page标记……
<Page Include="Views\MyCustomControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
如果您正在使用Xamarin Forms,并且您移动了一个XAML文件,那么该文件的“构建操作”将被更改。Xamarin Forms要求“构建动作=嵌入式资源”。
在Visual Studio中应用“build action”:
选择XAML文件-> Properties -> Build Action = Embedded Resource
当我不小心从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">
我知道这不是最初问题的答案(因为这是在另一台机器上构建的项目),但错误消息是一样的,所以也许我会帮助别人解决这种情况。
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工作正常一样。