Forum Xamarin.Forms

InitializeComponent does not exist in the current context error



  • JoshuaTantonJoshuaTanton USMember ✭✭

    For me I just had to uninstall Xamarin.Forms on my iOS project (the one the error indicated the method didn't exist for). Then re-install (the specific version I was looking for). This happened to me after I tried to roll back an update to Xamarin.Forms while trouble shooting errors with Bitrise.

  • @Leandro.9527 said:
    Ganesh Cauda's solution works for me. I copied 2 xaml to my project, and must change build action first.

    Me too, in my case i renamed a xaml page....and i had the error... build was the solution! Thank you!

  • shfarazishfarazi Member

    Add x:Class="Yourprojectname.Views.YourprojectPage" at the top of xml page above the Title. Hope this helps you :smile:


    <?xml version="1.0" encoding="UTF-8"?>
  • dwaynemclarendwaynemclaren USMember ✭✭

    @Xigmond said:

    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

    this solved the problem just correct the x:Class Attribute to be the same as the namespace

  • ajaykeshriajaykeshri Member ✭✭

    The namespace and class names need to match so they are both part of a partial class

    namespace XXX

    /// Interaction logic for MainWindow.xaml
    public partial class MainWindow

  • DannybangaDannybanga Member ✭✭

    Press CTR + S, it saved my Xaml file, cleaned my project then built it and it worked.

  • superdevdavesuperdevdave Member

    For me it was the wrong x:Name namespace I used in the xaml code of the page because I had changed namespace in code behind.After that I had to change build action of page in properties to Content and back to Embedded Resource again for the error to go away

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @Xigmond I know this is old, but you are a genius

  • smartboytksmartboytk SLMember
    edited December 2020

    The solution that WORKED was.
    1. Make sure the x:Class name in the xaml file is spelled correctly as in the public partial class
    2. The filename eg. OnboardingStyles.xaml must be the same name in the x:Class (i.e. OnboardingStyles) and the partial class (i.e OnboardingStyles)

    Save the files and the error was gone.

  • PratikPatilPratikPatil Member ✭✭

    I am facing the same problem even tried saving it in xml but cant resolve it please help

Sign In or Register to comment.