InitializeComponent does not exist in the current context error

Hi,

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.

Regards,
Andy

Posts

  • AndyWhitworthAndyWhitworth USMember ✭✭

    Thanks.

    That seems to have cleared the InitializeComponent error.

    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.

    Any ideas on this?

    Regards,
    Andy

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭

    Brendan Zagaeski gave some advice how to workaround this in bug 32988. See comments 18 and 23.

  • AndyWhitworthAndyWhitworth USMember ✭✭

    These issues seem to have been resolved for me by updating to the latest Xamarin.Forms NuGet package (2.2.0.5-pre2 at time of writing).

  • pallavboharapallavbohara USMember ✭✭
    edited February 24

    Mee too getting this error....any update on this?

    I just creadted a new project using MasterDetail Template.

  • RyanThomas.2503RyanThomas.2503 USMember

    Madlink's solution worked for me. Remember it is case sensitive too.

  • Leandro.9527Leandro.9527 USMember

    Ganesh Cauda's solution works for me. I copied 2 xaml to my project, and must change build action first.

  • DevologyDevology GBMember ✭✭

    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.

  • DFoulkDFoulk USMember ✭✭

    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)

  • MichaelVowlesMichaelVowles USMember ✭✭

    @Ganesh.Cauda said:
    In my case the problem is the build action, it should be : Embedded resource

    You sir, are awesome! I had added existing classes and they were imported as pages not embedded resources.

  • I got this error changing the namespace of the .cs file without updating it in the .xaml "x:Class" attribute.
    I wanted to change the namespace because I put all my pages inside a "pages" folder.

    If you moved the page to a subdirectory you should then update its namespace to reflect the new path.
    Remember to rename it in both the .xaml.cs and .xaml files.

  • GonzaloMartinGonzaloMartin USMember

    @FernandoSilva_lise said:

    @monto said:
    Only the following step helped me, saving or changing the Custom build tool didn't work. In package manager console, issue the following command:
    update-package -project your_pcl_project_name -reinstall

    The build is successful thereafter but the intellisense error still exists. But I can live with that for the time being.

    That worked 100% for me. Thanks!

    Worked 100% for me too. Thanks!

  • PhilSeemanPhilSeeman USMember ✭✭

    @Devology said:
    The only way I could get past this was to right-click on each XAML file list, choose properties, then change the build action to 'content' then back to 'Embedded Resource'.

    This worked for me - thanks @Devology!

  • JohnRattzJohnRattz USMember

    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").

  • GeetSGeetS USMember ✭✭✭

    I had updated Nuget package and the issue is sorted.

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭

    I get rid of this by manual Clean(remove bin and obj folders from all projects) and Rebuild.

  • tafftaff USMember ✭✭

    @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

    Worked for me too! Thanks, was pulling my hair out!

  • TinPTBTinPTB USMember
    edited September 6

    @FernandoSilva_lise said:

    @monto said:
    Only the following step helped me, saving or changing the Custom build tool didn't work. In package manager console, issue the following command:
    update-package -project your_pcl_project_name -reinstall

    The build is successful thereafter but the intellisense error still exists. But I can live with that for the time being.

    That worked 100% for me. Thanks!

    update-package -project your_pcl_project_name -reinstall

    That worked 100% for me. Thanks!

  • dloperabdloperab COMember

    @VolodymyrSavchenko works for me! Thanks!

  • XigmondXigmond USMember

    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 <3

Sign In or Register to comment.