Quick Links

InitializeComponent does not exist in the current context error

The error CS0103 (The name 'InitializeComponent' does not exist in the current context) has started appearing after doing a build of our Xamarin Forms solution. The build, however, appears to have succeeded in spite of this error message?

This started happening after adding the first XAML ContentPage to the PCL project. If I remove the XAML ContentPage then the error disappears.

I've tried installing the latest Alpha channel update on both my Windows (Visual Studio) machine and my Mac build host but it hasn't made any difference.

Please advise ASAP as we would like to use XAML for our ContentPages but may have to revert to using C# code to build the UI if the XAML approach is not viable.

However, we have a PCL class library project which is being utilized by our main PCL project. A similar issue is being observed with relation to this - errors are being raised indicating that the main PCL project is unaware of the types etc. exposed by the PCL class library project. Again, the build appears to succeed after further examination.

This is probably worse than the false InitializeComponent error as there could be many errors raised related to the PCL class library whereas there would only be one InitializeComponent error per XAML ContentPage and at least the errors would look the same.

Something similar happened to me when I moved the recently create page to the correct namespace. You need to keep in mind that the Page.xaml has a reference to the Page.xaml.cs so you need to also update the x:Class attribute on the ContentPage to match the new namesapce

For what it's worth - I had this error today (I used a PCL approach) and simply went to the top-level solution node and updated the Xamarin Forms nuget package for all the projects. After that I cleaned and rebuilt and it worked no problem.

Wow, this was a weird one... I made a little PowerShell script to "clean" my forms apps (it deletes all obj, bin & packages folders in the solution), then fired my solution up and attempted to deploy... All references appeared to be installed properly. I tried the solution provided by @VladislavAdvahov (opened my App.xaml file and saved it)- and magically my app deployed without error. Seriously- why did that work?! (scratching head)

I have also found unloading and reloading the individual projects (UWP, Android, iOS) for which this applies to fix the problem sometimes (only when VS just thinks this error exists - the project usually builds and runs fine for me whenever this removes the "error").

@Madlink said:
Something similar happened to me when I moved the recently create page to the correct namespace. You need to keep in mind that the Page.xaml has a reference to the Page.xaml.cs so you need to also update the x:Class attribute on the ContentPage to match the new namesapce

99% This problem is because you copied some code from your other projects or other resources and just forgot to make all the necessary changes to fit your own project !

well, the most important thing is to consider .xaml file and its** .cs** C# code-behind are just different parts of one class and that's why in .cs you declare the page class as : public **partial** class PageNameCls : PageType

So when the code-behind.cs Runs: InitializeComponent(); it expects to find the other parts of the class in the same NameSpace .

you just have to check the x:Class attributes in your page .xaml to be the same as the namespace you are using in your .c code-behind file and make sure its saved so the C-sharper knows your changes even-though it has no difference when you debug your project because the build process is set to save all files by default (visual studio 2017 )

> @Xigmond said:
> Hi
>
> 99% This problem is because you copied some code from your other projects or other resources and just forgot to make all the necessary changes to fit your own project !
>
> well, the most important thing is to consider .xaml file and its** .cs** C# code-behind are just different parts of one class and that's why in .cs you declare the page class as :
> public **partial** class PageNameCls : PageType
>
> So when the code-behind.cs Runs: InitializeComponent(); it expects to find the other parts of the class in the same NameSpace .
>
> you just have to check the x:Class attributes in your page .xaml to be the same as the namespace you are using in your .c code-behind file and make sure its saved so the C-sharper knows your changes even-though it has no difference when you debug your project because the build process is set to save all files by default (visual studio 2017 )
>
> that's all.
>
> hope it solve your problem