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

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

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

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

怎么了?


当前回答

在我的案例中(NET Core 3.1),我通过在项目文件的ProjectGroup部分中给它一个AssemblyName标记来修复它。如。

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
    <AssemblyName>ProjectNameUsually</AssemblyName>
  </PropertyGroup>
  ...
</Project>

这也修复了编译器在代码背后看不到x:Name控件的问题。

其他回答

对于那些在调试模式下没有错误,但在发布模式下有指定的错误(但项目运行正常)的人来说,这里有一些简单的尝试:

打开与出现问题的XAML .cs文件对应的XAML文件。 进行编辑——任何编辑,比如在某处添加空格 保存文件并关闭

这种方法在VS 2015中对我有效,根据其他用户的说法,在2017年和2019年也是如此

其他选项:

在我使用Xamarin Forms的情况下,在“InitializeComponent”的顶部,在我的App.xaml文件中的当前上下文错误中不存在,我还注意到在输出中,.csproj文件未能构建。奇怪。

除了下面两个ItemGroup条目外,文件本身没有什么特别引人注目的:

  <ItemGroup>
    <EmbeddedResource Remove="App.xaml" />
  </ItemGroup>

  <ItemGroup>
    <ApplicationDefinition Include="App.xaml" />
  </ItemGroup>

我从.csproj文件中删除了这两个文件,执行了清理和重建,错误最终消失了。解决这个问题很痛苦,也许这将帮助其他人避免一些压力。

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

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

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

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

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

这为我解决了问题。

我已经注释掉了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
}

因为这似乎是关于缺少'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%遇到这个问题的人)有所帮助。