Thursday, February 19, 2015

Xamarin.Forms - "InitializeComponent() was not found" error in a XAML page

I recently ran into an issue in Xamarin.Forms where previously working XAML (within a Content Page XAML) just stopped working and the following error was raised in Xamarin Studio:

InitializationComponent() was not found

I did some research and found that the myProject.projItems (*) file in the Xamarin.Forms project was changed and the XAML Content Pages nodes were no longer setup correctly.  Somehow Xamarin Studio changed the file and the XAML page nodes were now:

<Page Include="$(MSBuildThisFileDirectory)Views\Controls\MyContentPage.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
</Page>

When I changed the nodes to match the following, the InitializeComponent() error went away.:

<EmbeddedResource Include="$(MSBuildThisFileDirectory)Views\Controls\MyContentPage.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
 </EmbeddedResource>

I then went through the file and changed the other XAML files, and restarted Xamarin Studio and the error went away.

(*) NOTE - My Xamarin.Forms project wasn't named "myProject", it is set to that name to simplify the illustration. :)