locked
The name 'InitializeComponent' does not exist in the current context RRS feed

  • Question

  • I am adding a wpf window to my library module (.dll) and it gives me the above error. Any idea?

    It works if I add a WPF window to an application. 

    Thursday, June 7, 2007 7:25 PM

Answers

  • Right -- I have that happen occasionally after renaming things (or changing the type from a Window to a UserControl, etc). I've only done half a rename/fix up.

    So for example,

    Code Snippet

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
        x:Class="SecurityVideoBuilder.MainWindow"




    Must match in the code behind file:


    Code Snippet

    namespace SecurityVideoBuilder
    {
      /// <summary>
      /// Interaction logic for Window1.xaml
      /// </summary>

      public partial class MainWindow : System.Windows.Window
      {


     
    Friday, June 8, 2007 1:15 AM
  • Sounds a lot like you forgot to add the following to your project file:

     

     

    somewhere near the first line, add the second (assuming it's a C# project?)

    Code Snippet

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

     

    Thursday, June 7, 2007 7:51 PM

All replies

  • Sounds a lot like you forgot to add the following to your project file:

     

     

    somewhere near the first line, add the second (assuming it's a C# project?)

    Code Snippet

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

     

    Thursday, June 7, 2007 7:51 PM
  • You will also get that error if the XAML namespace/class definition and the code-behind  definitions do not agree. Even though you may have the correct targets in the .cs/vbproj files.
    Thursday, June 7, 2007 9:30 PM
  • Right -- I have that happen occasionally after renaming things (or changing the type from a Window to a UserControl, etc). I've only done half a rename/fix up.

    So for example,

    Code Snippet

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
        x:Class="SecurityVideoBuilder.MainWindow"




    Must match in the code behind file:


    Code Snippet

    namespace SecurityVideoBuilder
    {
      /// <summary>
      /// Interaction logic for Window1.xaml
      /// </summary>

      public partial class MainWindow : System.Windows.Window
      {


     
    Friday, June 8, 2007 1:15 AM
  • Thank you, it solved the problem
    Monday, June 18, 2007 10:27 PM
  • Hi,

    I have added <Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" /> to my project file but after that in my form design itys gives error like

     

    One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.

    Unspecified error
    Hide    

    at EnvDTE.CodeTypeRef.get_CodeType()
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.GetUrtTypeFromVsType(CodeTypeRef vsType)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnTypePopulateMembers(Object sender, EventArgs e)
    at System.CodeDom.CodeTypeDeclaration.get_Members()
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.MergedCodeDomParser.CodeTypeDeclarationPopulator.OnPopulateMembers(Object sender, EventArgs e)
    at System.CodeDom.CodeTypeDeclaration.get_Members()
    at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

    Tuesday, August 21, 2007 9:50 AM
  • Put your WPF in a separate control library and add a reference to it in your Windows Forms application. The csproj file for the WPF will contain the WinFX.targets and the Windows Forms one will not contain it. The form will then display cleanly.

     

    Wednesday, November 7, 2007 12:05 PM
  • I had the same problem.  I found you can avoid the error by creating the WPF object in the constructor instead of declaring it as a class member..
    Sunday, January 11, 2009 10:12 PM