如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误

名称“InitializeComponent”在当前上下文中不存在。

今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。

我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。

怎么了?


当前回答

.xaml文件的构建动作也必须设置为“Page”,当在项目之间移动xaml文件时,这个设置会丢失(至少在VS 2010中是这样)。

其他回答

我在把沙玛林的溶液转化成毛伊岛。

我需要更改MyPage。xaml文件属性/构建操作

来自“嵌入式资源”(在Xamarin中使用的方式)

到“MauiXaml”(在毛伊岛使用)。

我还必须清除属性/自定义工具。

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!

这和我之前做的顺序不一样。它需要被简化。

我也有同样的问题,我必须把我的共享项目转换成一个可移植的类库。

这发生在我身上,因为一个Nuget包卸载程序吹走了App.xaml中<Application>元素上的所有属性。这包括x:Class属性,它指定应用程序类名。因此从未生成包含InitializeComponent()方法的分部类。

我通过将App.xaml恢复到源控制副本来解决这个问题。

给那些在网上找到这个的人。检查窗户。Csproj文件,如果那里有编译。应该有2项

<Page Include="YourFile.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
</Page>

<Compile Include="YourFile.xaml.cs">
  <DependentUpon>YourFile.xaml</DependentUpon>
</Compile>