After porting an old Silverlight application, I started getting compiler errors stating “The name ‘InitializeComponent’ does not exist in the current context”.
This was baffling and after poking around a bit, this is how I resolved it.
Note: My solution may be specific to my Silverlight installation and computer setup. I never quite know what the various bits are.
Essentially, the build is not generating the hidden partial classes named foo.g.cs for the application or page foo.xaml. These contain the InitializeComponent and are hidden files under the obj/Debug folder.
To fix the problem, right-click on the project with the errors and select “Unload”
Then right-click and select “Edit xxx”. This will load the project file xml in the IDE.
Locate the item <ItemGroup>, which is somewhere towards the bottom.
You will see blue squiggly lines under the Application element and the Page.
Change them to be <ApplicationDefinition Include=xxx> and <Page Include=xxx>
Note: There will be a blue squiggly line under <SilverlightApplication>true</SilverlightApplication>, but no need to fix that
Save the xml and right-click the Project and select Load. Say ok to unload open file and load and compile project.” Hopefully your problem was resolved.
December 30, 2009 at 4:11 pm
I had a similar problem. In looking as the .csproj file I noticed the Silverlight tag had the blue squiggly. The line above was the framework version which was set to 2.0. Changing to 3.5 made the problem go away.
Since everything was generated, I am not sure how the 2.0 got in there.
December 30, 2009 at 5:25 pm
weird that the framework version had changed. i have no idea why. Glad you found a fix.
October 3, 2011 at 5:27 pm
I was receiving this error but found that it was due to another cause. I had accidentally removed the x:Class=”Namespace.ClassName” attribute in the XAML for my UserControl. Once I put that back in and did a rebuild, it began to work again.
June 23, 2012 at 7:38 pm
thank you very much- solved my problem!
September 14, 2012 at 11:53 am
thanx a lot