Answered The name 'InitializeComponent' does not exist....

  • Tuesday, September 12, 2006 8:06 PM
     
     

    I'm trying to create a UserControl in a Class Library and it all works just fine. I can see everything in designer, but when I'm trying to build my solution I get the following error.

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

    And that's it, as it says I don't have that method in my .cs codebehind file. But the thing is when I create a UserControl in a WinFX Windows Application I can build it just fine, so i must be missing something. I tried removing the method call and that let me build it, but when creating a new instance and adding it to my Grid nothing happend. 

    Any idea what I'm doing wrong.

    I am somewhat new to the .NET platform ( great idea jumping on the .NET 3.0 wagon then huh  ) But I would really like some help on this issue, because I do not have the slightest idea where to go from here.

All Replies

  • Tuesday, September 12, 2006 8:26 PM
    Moderator
     
     Proposed Answer

    The initializeComponent routine is created in the .g.cs file that is created during build.  (see obj\debug\...)

    Make sure your XAML file has a build action of "Page" to create that file.

     

    Also, make sure that the partial classes in .xaml.cs and .g.cs match.

    Hope that helps.

    Thanks, Rob Relyea
    WPF Team
    http://rrelyea.spaces.live.com

     

    • Proposed As Answer by Prince Adusei Tuesday, October 26, 2010 6:25 AM
    •  
  • Tuesday, September 12, 2006 9:41 PM
     
     Answered

    Thank's Rob, but it didn't seem to help, everything was set up as you said.

    I cruised a bit more around and stumbled over this

    http://blogs.msdn.com/mhendersblog/archive/2005/10/03/476536.aspx ( something about hosting WPF in WinForms, quite clever actually  )

    I't seems that when creating a DLL project (as with WinForms) Visual Studio can't handle "WPF/WinFX file types", so all I had to do was add <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" /> in my .csproj file.

    Now it works like a charm

    PS. thank's for keeping us up to date on the RC1 and Sept. CTP releases

  • Tuesday, September 12, 2006 11:48 PM
    Moderator
     
     

    Ah, yes, I should have noticed you said "Class Library".

    If you use the Wpf Custom Control Project, that will have been done for you.

    Thanks, Rob

  • Saturday, March 17, 2007 8:15 PM
     
     

    Rob, we need better discoverability on this. The error message needs to do a better job at guiding us to the solution.

    Update: I'm not seeing a "Wpf Custom Control Project" project option inside VS 2005. In the Add New Project dialog I only see:

    Windows Application
    Class Library
    Windows Control Library
    Windows Application (WPF) 

    And several other objects that are obviously not relevant.

  • Sunday, March 18, 2007 2:02 PM
    Moderator
     
     

    Hey Mark-

    With the VS 2005 extensions, you are looking for "Custom Control Library (WPF)" which is not listed in the main nodes (C# or VB), but can only be found under the "Net Framework 3.0" node of Project Types.

    In VS "Orcas" March 2007 CTP, VB & C# are a bit different:

    • VB:  Under the "Windows" node of VB projects, there is "WPF Custom Control Library" and "WPF User Control Library".
    • C#: Under the top level node and the "Windows" node, there is "WPF User Control Library" available.

    (my guess is that the orcas one may change a little bit...)

    Hope that helps.

    -Rob

  • Monday, January 05, 2009 11:20 AM
     
     
    Just came across this compile error when I created a new WPF user control in VS 2008.  I had used Visual Studio's 'Refactor' to change the class name, but it failed to rename the class name in the XAML (only in code-behind).  Changing the class name there to match fixed the problem. 
  • Friday, October 22, 2010 6:24 PM
     
     
    Just came across this compile error when I created a new WPF user control in VS 2008.  I had used Visual Studio's 'Refactor' to change the class name, but it failed to rename the class name in the XAML (only in code-behind).  Changing the class name there to match fixed the problem. 


    This clued me into why I was having this very same issue.

    I had moved my controls to a lower namespace, and the XAML was still referring to them in the old namespace. So if you move your controls from one namespace to another, make sure you update your namespace references in the XAML, or you'll get this very error message!

  • Friday, December 17, 2010 4:38 AM
     
     

    In my case, the problem was in a type of "Build Action".

    When I copied & pasted the window from another project it took for some reason "Resource" build action. I fixed problem by changing it to the "Page".

  • Monday, June 20, 2011 7:11 AM
     
     
    Same here Monsignor - changing from 'Resource' to 'Page' fixed the problem.
    • Proposed As Answer by iahmed2011 Wednesday, June 22, 2011 3:36 PM
    • Unproposed As Answer by iahmed2011 Wednesday, June 22, 2011 3:37 PM
    •  
  • Wednesday, June 22, 2011 3:38 PM
     
     
    for me it was a different class name in the xaml at the top where it says x:Class. Interesting that it was a typo and these are hard to find. When you guys were talking about the .g file I thought may be I should check the class name in xaml