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

    Question

  • Right I have looked through the fourms to see if this question was perviously asked and it appears that it has but, I have failed to see where they have been answered:  I have deleted and re-installed my Silverlight files necessary for dveloping silverlight app in visual studio.  I'm using VS 2008, and have created a simple silverlight app called Howdy Silverlight.

     

    I have changed the .csproj  adding :

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

    and I still get the following error:

        Error    2    The name 'InitializeComponent' does not exist in the current context    C:\Users\DM\Documents\Visual Studio 2008\Projects\HowdySilverlight\HowdySilverlight\Page.xaml.cs    18    13    HowdySilverlight

     

    The page.xaml:

    <Canvas x:Name="parentCanvas"
            xmlns="http://schemas.microsoft.com/client/2007"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Loaded="Page_Loaded"
            x:Class="HowdySilverlight.Page;assembly=ClientBin/HowdySilverlight.dll"
            Width="640"
            Height="480"
            Background="White"
            >

    </Canvas

     

    and the page.xaml.cs:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace HowdySilverlight
    {
        public partial class Page : Canvas
        {
            public void Page_Loaded(object o, EventArgs e)
            {
                // Required to initialize variables
                InitializeComponent();
            }
        }
    }

    The following web references:

    agclr, mscorlib , system, system.core, system.silverlight, system.xml.core

    are also referenced.

     

    Would greatly appericate a solution this problem,

    Regards 

    Thursday, January 31, 2008 12:48 PM

Answers

  • The Import you're using is for WPF and silverlight is not WPF at all.  The initialize component is created from the XAML file's code generator.  If you don't have it correctly setup, it won't build the .g file that is necessary.  Typically this is making sure that the "CustomTool" of the .xaml file says "MSBuild:CompileXaml"

    Thursday, January 31, 2008 1:09 PM

All replies

  • The Import you're using is for WPF and silverlight is not WPF at all.  The initialize component is created from the XAML file's code generator.  If you don't have it correctly setup, it won't build the .g file that is necessary.  Typically this is making sure that the "CustomTool" of the .xaml file says "MSBuild:CompileXaml"

    Thursday, January 31, 2008 1:09 PM
  • What .g file.  ? and what which customTool do you refer?  None of the tutorials on silver.net make mention of this, so could be a bit more specific. 

    Thursday, January 31, 2008 1:33 PM
  • OK, I'm calm now, and understand the MSBuild:CompileXaml Options setting, silliy overlook on my part! Thanks 

    Thursday, January 31, 2008 2:24 PM
  • Sorry, fi you have the MSBuild:CompileXaml set in your .xaml file, when you save the file it will build a page.xaml.g file in the obj directory that it uses during compilation.  This is the generated part of the code.

    Thursday, January 31, 2008 4:31 PM
  • You also get this error if you rename the class and forget to update the XAML.

    Thursday, December 11, 2008 5:33 PM
  • IWolf is absolutely right. Thanks :P

     

    Friday, February 27, 2009 3:38 PM
  •  I also have the same problem. Can you please tell me where did you do changes, in what files and fill attributes and what values you set since I am new to Silverlight

    Wednesday, August 05, 2009 1:46 PM
  •  http://techkn0w.wordpress.com/2009/08/22/silverlight-error-the-name-initializecomponent-does-not-exist-in-the-current-context/

     

    worth a read also.

    Sunday, April 25, 2010 2:26 PM
  • I get the same error, and i found this page:

    http://timheuer.com/blog/articles/silverlight-get-started-part-4-binding-data.aspx

    Darren said:

    [Figured out my issue. Sometime in the course of coding, I must have deleted the inital x:Class="TwitterSearchMonitor.Views.Search" from the first line of my Search.xaml. I put that back in and it's working now. :)]

    And i fixed the error with the same way.

    Hope this help.


    Thursday, April 29, 2010 9:19 AM
  •  Post time 5/17/2010

    If people have come across this and have not gotten a solution through this post I have come up with a possible fix.

    My configuration:
    Language: VB
    Tools: Visual Studio 2008 and MS Blend 2

    This same error can be replicated if you assign an event handler to an object in your xaml file but do not have an event in your xaml code. behind. This can occur on occasion when using MS Blend to assign events and visual studio is slow in responding or you make two event changes in in Blend without updating and apply the changes in visual studio.

     

    Example:
    xaml code:
    page.xaml

    <TextBox x:Name="txtURL" Text="None" Height="24" Margin="0,0,160,0" VerticalAlignment="Top" TextChanged="txtURL_TextChanged"/>

    page.xaml.vb

      Private Sub txtDebug_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)

        End Sub

     

    In the above case page.xaml.vb has an event handler for txtDebug but not for txtURL. This original posters error concern can be replicated when using this description and silverlight 2

     

     

     

     

     

     

    Tuesday, May 18, 2010 12:58 AM
  • I solved my problem by fixing the namespace difference between my xaml and cs file.

     

    Friday, June 17, 2011 3:55 PM
  • Bingo - thanks for the tip. I had copied some Xaml files from the Sliverlight Navigation template-generated project. I changed the namespace in the .cs files, but neglected to update the namespace in the first line of the Xaml file.

    Thursday, July 12, 2012 3:17 PM
  • ditto ... I had changed the app.xaml.cs namespace but did not immediately recongize the the spot for that namespace in app.xaml. 

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="<put your app.xaml.cs namespance here>"
    >

    Thursday, September 13, 2012 6:56 PM