Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102

Hi.

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!

Amit

Tags: , , , , , ,

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


  1. Michiel Said on Jan 31, 2008 :

    Followed to the letter and the error still persist?

  2. admin Said on Feb 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 Feb 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 Feb 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 Feb 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 Mar 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 Jan 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 Feb 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 Oct 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 Jan 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
    http://social.msdn.microsoft.com/Forums/fi-FI/wpf/thread/252b94c3-604f-49d6-9edb-d5334862034c

    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 Mar 19, 2010 :

    This worked for me, thanks!

  13. uchenna Said on Mar 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.

1 Trackback(s)

  1. Mar 14, 2008: My Blog Stats - February 2008 | Dev102.com

Post a Comment

Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More