Xamarin Error: InitializeComponent does not exist in the current context

By in ,
No comments

If you are working with Xamarin, perhaps in a shared environment, you may come across a new Forms Xaml Page that is in the file system but not yet included in your project. When you proceed to “Include in Project”, you may then encounter this error in the constructor of the ContentPage:

InitializeComponent does not exist in the current context

It turns out that when you add a Forms Xaml Page in Visual Studio it makes some additional changes to your .csproj file to compile the xaml control along with the partial class. It looks something like this:

<ItemGroup> <EmbeddedResource Include="Views\MyXamarinFormPage.xaml"> <Generator>MSBuild:Compile</Generator> </EmbeddedResource> </ItemGroup>

You can add this entry manually to resolve the error. In my case, I simply copied out the file to a temp location, then added the Forms Xaml Page to the project using Visual Studio, and finally copied the content back onto the page and the error went away. I hope this is helpful!

The following two tabs change content below.

selaromdotnet

Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.