Ever had the following error in Visual Studio:error CS0103: The name ‘InitializeComponent’ does not exist in the current context.I sure did…

There’s a very specific reason for this and it’s in the project settingsThis usually happens whenever you try to add a WPF control/window to a .NET 2.0 Class library or project. The reason for this error is that the project does not know it’s building a WPF Control or Window and therefore tries to build it as a C# 2.0 project. The solution involves editing the .csproj file.Right click on the project causing the problem and select “Unload Project”. Right click the unloaded project and select “Edit <projname>.csproj”The .csproj file will open and you can see the xml. look for the following line:

<Import Project=…..

It’s near the end of the file. and the only line that you have is probably this:

<Import Project=$(MSBuildBinPath)\Microsoft.CSharp.targets/>

This tells Visual Studio to build the project as a .NET 2.0 project. What we want to do is to tell Visual Studio that this is actually a WPF project, so we have to add the following line:

<Import Project=$(MSBuildBinPath)\Microsoft.WinFX.targets/>

This line will tell Visual Studio to build the project as a WPF project. Now your .csproj file bottom should look like this:

< Import Project=$(MSBuildBinPath)\Microsoft.CSharp.targets/>
<Import Project=$(MSBuildBinPath)\Microsoft.WinFX.targets/>

Save the .csproj file, right click it in the solution explorer and select “Reload Project” Compile and that’s it, your all done!


Tags :

22 Responses to “How to Solve error CS0103: The name ‘InitializeComponent’ does not exist in the current context”

  1. Michiel

    Said on January 31, 2008 :

    Followed to the letter and the error still persist?

  2. admin

    Said on February 6, 2008 :

    you might have a problem of namespaces.

    check that the namespace of the .Xaml file and the namespace of the .Xaml.cs file are the same

  3. Steve

    Said on February 20, 2008 :

    Did this, but now I get the error

    There is a circular reference involving the import of file “C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.WinFX.targets”. This file may have been imported more than once, or you may have attempted to import the main project file. All except the first instance of this file will be ignored.

    And the problem persists. Maybe it’s a bad idea to add a WPF project to an existing solution?

  4. Brandon

    Said on February 25, 2008 :

    Another reason for this error is that the xaml file cannot be compiled to baml. Usually this is because of a typo or duplicate x:Name attributes, i.e. . If you look under the obj directory for the project and notice the .baml file is not being generated, check your syntax.

    Hope this helps, almost everytime I get this error it is because of a typo in my xaml.

  5. admin

    Said on February 25, 2008 :

    that depends on the type of the solution you are adding the WPF project to.
    What are you trying to do?

  6. Kalmi

    Said on March 12, 2008 :

    for me it was a namespace problem

  7. Jeff

    Said on May 15, 2008 :

    It was a namespace problem for me too.

  8. sezai yükseketepe

    Said on January 5, 2009 :

    Right friends.problem was at namespace.I will chance namespace path.. problem solved.By the way I love you Hagi. :)

  9. Josh

    Said on February 22, 2009 :

    I have the same problem with a VB project. I added a WPF window (.xaml) file, and ever since, when i come back to my project, all of my old .vb forms and code are messed up. My main form design doesnt show up. Main.vb, instead it has main.resx underneath it instead of the code file. How can I fix this?!

  10. Venkata

    Said on October 11, 2009 :

    I compiled the project after renaming the Namespace using Refractor.
    I got the error ‘InitializeComponent’ does not exist in the current context.

    Renaming the NameSpace in .cs code does not change in .xaml file.After manually renaming namespace in .xaml file the project is compiled successfully.

  11. dwight

    Said on January 1, 2010 :

    I am having the same problem. And it does not appear to be a namespace issue. After having this problem with 1 project, I copied in the project found at

    After building the project in VS Studio 2008, I get three errors.

    InitializeComponent does not exist in current context.

    WpfMania.GadgetWindow does not contain a definition for Window_MouseLeftButtonDown

    WpfMania.GadgetWindow does not contain a definition for Button_Click.

  12. Simon

    Said on March 19, 2010 :

    This worked for me, thanks!

  13. uchenna

    Said on March 30, 2010 :

    To enable the details of this specific error message to be viewable on remote machines, please create a tag within a “web.config” configuration file located in the root directory of the current web application. This tag should then have its “mode” attribute set to “Off”.

    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the “defaultRedirect” attribute of the application’s configuration tag to point to a custom error page URL.

    I replaced the “RemoteOnly” with “Off”

    this is the new error am having now

    Server Error in ‘/Mayflowerblog’ Application.
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name ‘ExtensionManager’ does not exist in the current context

    Source Error:

    Line 53: foreach (SortedExtension x in sortedExtensions)
    Line 54: {
    Line 55: if (ExtensionManager.ExtensionEnabled(x.Name))
    Line 56: {
    Line 57: a.CreateInstance(x.Type);

    Source File: e:\web\mayflowerpo\htdocs\Mayflowerblog\Global.asax Line: 55

    Please what do I do?

  14. sujit

    Said on May 5, 2010 :

    Hay Thanks a lot, it worked.

  15. hassan

    Said on July 27, 2010 :

    i m using vs2010 trial; sivlrlght navigation proj; i added pp.xaml
    silvrlght page i cut and pasted xaml code which supposed to done in expression blend ; then i got the error and nothing helped.
    the name space of my .cs is twittersearchmonitor when i looked into xmlns in the xaml there are a bunch of them so whcih one to change or delete or add

  16. Indrajith

    Said on August 6, 2010 :

    Thanks a ton. I’ve been struggling to resolve this error for quite a while now.

  17. Q

    Said on August 16, 2010 :

    Hi guys I’m new to c# and also new to Visual studio
    I’m getting the same error
    The name ‘InitializeComponent’ does not exist in the current context”
    hoping namespace is the problem for me too
    but not sure how to change the .xaml and .xaml.cs namespace you guys are talking about

    how do i open those files in vs??

    any comment will be appreciated!

  18. All Gods bless you

    Said on March 2, 2011 :

    Thank you very much. It solved my issue on VS2008 express edition.

  19. pixtur

    Said on April 17, 2011 :

    Just spent two hours on this problem, just to figure out, that my problem was caused by the order in which you define Resources. This code worked:

    However, defining the Converter above the Array will result in the InitializeComponent error.

  20. Danish

    Said on November 27, 2011 :

    Thanks :D that was really helpful! Bless you!

2 Trackback(s)

  1. Mar 14, 2008: My Blog Stats - February 2008 | Dev102.com
  2. Jun 25, 2012: [Development] WPF fails?

Post a Comment