Xamarin Forms - InitializeComponent does not exist in the current context


I was presented with this error whilst developing with Xamarin Forms





For me it was simply down to copy and paste :-O. 

I'd created a new Xaml Page, which fully qualified was MyNameSpace.MyPage. I then copied in some Xaml which had a different name in the Xaml x:Class attribute (x:Class="NotMyNameSpace.OrPage"). I then tried to compile with our fixing the x:Class attribute and got the 'InitializeComponent does not exist' error. Once I changed the Xaml to match my actual classname (x:Class="MyNameSpace.MyPage") I was back on track and the error went away. Hope this helps.

For more information have a look at forums.xamarin.com

The other reason is that you may have created a Shared Xamarin Forms Project rather than a Xamarin Forms Portable project.

From Craig Dunn at Xamarin (taken from Xamarin Forms Forum)

You cannot use Xaml with the Shared Project template with iOS apps (in Xamarin Studio).

It's a weird combination to remember, I know, and we want to fix it... but for now, if you want to use Xaml files across all the platforms you should use the PCL template. Shared Projects are still fine if you are happy writing your UI in code.

The iOS build tool chain in Xamarin Studio doesn't (yet) know enough about the .xaml files to generate the codebehind/partialclass... therefore no backing class and InitializeComponent method is generated. This is why you get the error that it cannot be found. Because Shared Projects push all their content into the referencing project to compile, the Xaml won't work for iOS.

PCL works because the way it is compiled "knows" about Xaml and generates the correct partialclass. The PCL assembly already has all the necessary stuff compiled-in before being copied to each application (including iOS).

Comments

Popular posts from this blog

Separator Insets on UITableView in iOS8 - Xamarin.iOS

System.UnauthorizedAccessException error with Visual Studio and Xamarin