InitializeComponent does not exist in the current context error

2»

Posts

  • 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:

    ex:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage 
        xmlns="..." 
        xmlns:x="..." 
        x:Class="ClientApp.Views.ClientPage"
        xmlns:vm="clr-namespace:ClientApp.ViewModels" 
        Title="Home">
    
  • dwaynemclarendwaynemclaren USMember ✭✭

    @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 <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
    {
    //...
    }
    }

  • DannieIbangaDannieIbanga 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

Sign In or Register to comment.