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

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

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

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

怎么了?


当前回答

问题出现时,我没有改变代码,使用最近的VS2019 16.7.5。

只要打开相应的XAML文件就可以解决这个问题。

其他回答

这为我解决了问题。

我已经注释掉了App.xaml文件中的资源

<Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Application.Resources>
    <!--<ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>-->
  </Application.Resources>
</Application>

将此注释回来以修复构建错误。

<Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

深入挖掘一下,我发现{Project}\obj\debug中的app.g.cs文件在我留下资源注释时只包含以下内容。

/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Uri resourceLocater = new System.Uri("/MyApp;component/app.xaml", System.UriKind.Relative);

    #line 1 "..\..\..\App.xaml"
    System.Windows.Application.LoadComponent(this, resourceLocater);

    #line default
    #line hidden
}

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恢复到源控制副本来解决这个问题。

(老问题了,但如果搜索把你带到了这里)

01/09/2022 -在visual studio社区2022经历了问题。

报告的问题是一个未更改的.xaml.cs文件。

今天对我有效的解决方法是:

复制了我一直在做的最后一个xaml文件(和xaml.cs)到一个不在项目树中的文件夹。

在解决方案资源管理器中右键单击文件并选择“删除” (这将删除文件)

重新构建项目(在引用文件的地方会出现错误)

将xaml和xaml.cs文件复制回项目文件夹

右键单击项目,选择“添加/现有项目”,并选择xaml文件。 重新构建项目

(对我来说,它构建和其他xaml文件上的错误已经消失了)

另一种可能的解释是,您正在针对x86进行构建。右键单击解决方案并选择配置管理器。看看你是否在x86而不是任何CPU上构建。