如果我在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!

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

其他回答

这为我解决了问题。

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

在这个线程中,MCVE的最佳射击是,使用VS2017 15.5.2,加载LabelControlAdvancedSample的XAML,这是本教程页面的最后一个示例。

<Window x:Class="WpfTutorialSamples.Basic_controls.LabelControlAdvancedSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LabelControlAdvancedSample" Height="180" Width="250">
<StackPanel Margin="10">
    <Label Target="{Binding ElementName=txtName}">
        <StackPanel Orientation="Horizontal">
            <Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_green.png" />
            <AccessText Text="_Name:" />
        </StackPanel>
    </Label>
    <TextBox Name="txtName" />
    <Label Target="{Binding ElementName=txtMail}">
        <StackPanel Orientation="Horizontal">
            <Image Source="http://cdn1.iconfinder.com/data/icons/fatcow/16/bullet_blue.png" />
            <AccessText Text="_Mail:" />
        </StackPanel>
    </Label>
    <TextBox Name="txtMail" />
</StackPanel>

在将App.xaml和App.xaml.cs设置为默认状态后,尝试编译上面的代码会产生链接器错误。 幸运的是,当鼠标悬停在LabelControlAdvancedSample.xaml.cs中的InitializeComponent()语句上时,会有一个链接文本提示:

显示可能的修复。

点击它会调用另一个链接文本:

生成方法MainWindow.InitializeComponent。

这样做会产生以下“do nothing”方法:

    private void InitializeComponent()
    {
        throw new NotImplementedException();
    }

必须为要构建的项目定义函数。看起来在WPF和VB.Net中InitializeComponent的实现有一些不同。 编辑:xaml第一行中的namespace.class不正确。 根据MSDN和@Sean B的回答,应该是

<Window x:Class="LabelControlAdvancedSample.MainWindow"

因此,项目编译没有错误,并且不需要虚拟的InitializeComponent方法,实际上它会产生更多的错误。去显示VS是有帮助的,即使在极其罕见的用户错误的情况下。: P

我同意上面的答案,即名称空间必须匹配。但是,我遇到了这样一个问题,即名称空间匹配。

为了解决这个问题,我简单地将XAML中的名称空间更改为不正确的名称空间,保存,然后将其更改为正确的名称空间。瞧!

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

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

I know this was answered due to a different cause, but this is a highly hit posting and I had ran into the same issue with a class library. In this case, it turned out to be both a change in my namespace (answered in this post here) and that the compiler could not rebuild the Window.g.i.cs which defines the InitializeComponent() method. It couldn't because the class library was missing the ProjectTypeGuid value for WPF projects in the csproj file. Instructions for this are here and here. I thought I would share in case someone else has run into the same issue. Just changing the namespace isn't enough in this case.