The name 'InitializeComponent' does not exist in the current context

If I create a new project in Visual Studio 2010 SP1 and select "WPF Application" and tries to build the generated application, I get the error

The name 'InitializeComponent' does not exist in the current context.

I got a similar error this morning when I tried to build my current project. Yesterday, I had no problem compiling and running it.

I created a new project and got the error whenever I compiled the project. I have just sent the project to a colleague, and he has just compiled without any errors.

What is wrong?


I've encountered this a couple times and keep forgetting what causes it. I ran into this when I renamed the namespace on my code behind file but not in my XAML.

So check if you've done the same.

The namespace and class names need to match since they are both part of a partial class

namespace ZZZ
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
         //...
    }
}

<!-- XAML -->
<Window x:Class="ZZZ.MainWindow">

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


For those who (in VS2015) have no errors in Debug mode, but do have the specified error in Release mode (and yet the project runs fine), here is something simple to try:

  • Open the XAML file corresponding to the offending xaml.cs file.
  • Make an edit--any edit, like add a space somewhere
  • Save the file and close it
  • This method worked for me.

    链接地址: http://www.djcxy.com/p/59264.html

    上一篇: GStreamer hello world program in visual studio 2010

    下一篇: 名称'InitializeComponent'在当前上下文中不存在