How to fix “the name ‘InitializeComponent’ does not exist in the current context” in Silverlight or WPF
The error messages:
You’ve made some changes to your Silverlight application and all of the sudden your MainPage.xaml will not compile because InitializeComponent does not exist in the current context or the LinksStackPanel.Children doesn’t exist in the current context. This is a frustrating problem until you know what causes it.
The root cause of the problem is that your x:Class directive in the “xaml” page somehow got lost or is not in sync with your code-behind file.
Did you just copy a MainPage from another project, or backed up project? Copying the MainPage.xaml and the code-behind doesn’t seem to fix the problem by itself.
First, fix the x:Class reference in your MainPage.xaml
Make sure that your x:Class is named properly and that the namespace matches that of your application! If you have already checked this then don’t worry there is one more thing that probably went wrong.
<UserControl x:Class="WordpressManagerPHP.MainPage" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" snipped the rest...
Second, fix the “Build Action” of your MainPage.xaml
This one took me several hours, and a few headaches to find. I compared a working project to the non-working one and notices that the “Build Action” of MainPage.xaml must have changed when manipulating the window. The Build Action was set to “Content”.
The Build Action should be set to “Page”
Simply highlight the MainPage.xaml in your Solution Explorer and in the properties check out the Build Action. Once you fix this, you will no longer have problems with InitializeComponent and it will fix problems with LinksStackPanel.Children not existing in the context.