Xamarin.Forms - InitializeComponent doesn't exist when creating a new page

53,136

Solution 1

UPDATE: This error doesn't usually appear in VS 2015, if it does, here's my original answer:

Found the solution! Right click on the .XAML file, select Properties.

You will see a Property called Custom Tool. Change its value from MSBuild:Compile to MSBuild:UpdateDesignTimeXaml

This will solve the problem. Dont know about the downvote, but here's my screenshot:

Screenshot UPDATE:

It reappears rarely. If it does, just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done.

Solution 2

I get this sometimes and here's the checklist that solved them so far:

  1. Make sure the namespace in .xaml and .xaml.cs match

  2. Inherit from the correct parent - ContentPage for a page and ContentView for a control

  3. Set build action of the .xaml file to Embedded Resource if in the shared project.

Solution 3

As far as my observation is concerned, in Visual Studio 2015, XAML properties are already set as suggested by highly-voted answers here by default, specifically :

  • Custom Tool = MSBuild:UpdateDesignTimeXaml
  • Build Action = Embedded Resource

but the error still appears sometimes... (like in this other question).

Editing the corresponding XAML file and then hit CTRL+S should work fine, but you don't have to. A cleaner way to force Custom Tools to be run is by right-clicking on the XAML file and then click on "Run Custom Tool" context menu.

enter image description here

Solution 4

Updating the Xamarin.Forms NuGet package should do the job

Solution 5

I have met this problem. It's associated with the encoding of XAML files in VS. I'm using VS2015.

I solved this problem as follows:

  1. Open the *.xaml file in the project and click Save button. (There will be applying the correct encoding in VS2015).

  2. Then reopen the project and rebuild it. Now there are no errors.

Share:
53,136
user2915962
Author by

user2915962

Updated on July 05, 2022

Comments

  • user2915962
    user2915962 about 2 years

    I'm using Visual Studio to try out Xamarin.Forms. I'm trying to follow the guide: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/getting_started_with_xaml/

    In short, I create a Xamarin.Forms solution using a PCL and then try to add a Forms XAML Page to the PCL-project.

    The code-behind that gets created looks like this:

        public partial class Page1 : ContentPage
        {
            public Page1()
            {
                InitializeComponent(); 
            }
        }
    

    The problem here is that InitializeComponent(); is red. When I try to build I get informed that The name 'InitializeComponent' does not exist in the current context

    I've been looking around for solutions and even though others have had the same trouble, their solutions wont work for me. Here is one suggestion i tried to use: http://blog.falafel.com/xamarin-error-initializecomponent-does-not-exist-in-the-current-context/

    Please let me know if you have a solution for this problem. Thanks!

    Update:

    My PCL (which is where I also want to add my XAML-page) contains:

    App.cs:

        public class App : Application
        {
            public App()
            {
                // The root page of your application
                MainPage = new ContentPage
                {
                    Content = new StackLayout
                    {
                        VerticalOptions = LayoutOptions.Center,
                        Children = {
                            new Label {
                                XAlign = TextAlignment.Center,
                                Text = "Welcome to Xamarin Forms!"
                            }
                        }
                    }
                };
            }
    
            protected override void OnStart()
            {
                // Handle when your app starts
            }
    
            protected override void OnSleep()
            {
                // Handle when your app sleeps
            }
    
            protected override void OnResume()
            {
                // Handle when your app resumes
            }
        }
    

    And my XAML-page:

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="XamaTest.MyXamlPage">
        <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    </ContentPage>
    

    Code-behind:

        public partial class MyXamlPage : ContentPage
        {
            public MyXamlPage()
            {
                InitializeComponent();
            }
        }
    
  • user2915962
    user2915962 over 9 years
    Thank you for the tip. Unfortunatly it did not change anything in my case.
  • Thomas Weller
    Thomas Weller over 9 years
    Hmm. I had the same problem and it worked for me. Are there any other packages in your solution?
  • user2915962
    user2915962 over 9 years
    No, just a "fresh" Xamarin.Forms app with the default PCL.
  • user2915962
    user2915962 over 9 years
    Found the solution here: blog.galasoft.ch/posts/2014/07/… Appearatly there is a bug when you create a new xaml-page. It automatically gets the extension .cs instead of xaml. I never noticed this. Thank you!
  • Nagama Inamdar
    Nagama Inamdar over 9 years
    Welcome to Stackoverflow. Use some formatting to make it readable format.
  • Richard Logwood
    Richard Logwood almost 9 years
    @user2915962 The NuGet package update and clean/rebuild (+restarts) did not fix the problem for me. After reading you comment, I decided to add a new Xamarin Forms XAML page. The "InitializeComponent" error on both pages went away after a build with the new page! Looks like a VS state issue of some kind. When the problem was there I could step through the compiler generated InitializeComponent code without a problem. I'm guessing that adding a new XamForms XAML page after the NuGet package upgrade helped sort out some VS internal state that a clean/rebuild did not, but that is unconfirmed.
  • Dushyant Bangal
    Dushyant Bangal over 8 years
    I had to do that too. But when I opened the old project made in VS 2013, the error came back! Projects created in VS 2015 don't show it. Maybe that will narrow the cause.
  • David
    David over 8 years
    I had 1 element/control that didn't have x:Name attribute on it..once i added that the error went away
  • vamyip
    vamyip over 8 years
    Worked for me, Thanks!!
  • Dushyant Bangal
    Dushyant Bangal over 8 years
    Good to know. You can mark it "useful" for the next guy by giving +1
  • Tony Trembath-Drake
    Tony Trembath-Drake over 8 years
    and for me - i needed 4. Property called Custom Tool. Change its value from MSBuild:Compile to MSBuild:UpdateDesignTimeXaml as said in another post here...
  • Dalbergia
    Dalbergia about 8 years
    Setting the build action to EmbeddedResource fixed this for me. I copied the .xaml and .xaml.cs files over from a different project, and the .xaml didn't have a build action set. So thanks for your checklist.
  • angularsen
    angularsen about 8 years
    Got the same on a clean new project from template. Changing and re-saving the code-behind and .xaml file fixed it for me.
  • frostymarvelous
    frostymarvelous about 8 years
    My problem was with x:Class. It seems it was still pointing at Page1
  • Simon K.
    Simon K. about 8 years
    This was set in my case, but the error did appear still.
  • Dushyant Bangal
    Dushyant Bangal about 8 years
    @SimonK. have you tried the re-saving trick? I haven't worked on Xamarin for a few months now, but looks like people are still facing this problem, and the trick is working for them too.
  • Simon K.
    Simon K. about 8 years
    Project clean and rebuild did it for me.
  • Ismapro
    Ismapro about 8 years
    Thought it was the solution actually I already had it implemented. I also tried the resaving trick and it did not work.
  • Dushyant Bangal
    Dushyant Bangal about 8 years
    @Ismapro , clean and build might help. Its such a sad thing they haven't fixed this yet.
  • jan hruska
    jan hruska about 8 years
    It works for me. Adding bug to blank project it is really amateur work from microsoft. It took me a lot of time to just compile hello world. God bless android studio
  • Aman Sura
    Aman Sura about 8 years
    Make sure the namespace in .xaml and .xaml.cs match.. This is an important point highlighed. Visual Studio sometimes do not autoupdate this.
  • sh1rts
    sh1rts almost 8 years
    This worked for me too ! Really hope this gets fixed by MS/Xamarin soon..
  • Veverke
    Veverke almost 8 years
    Getting it when trying to run Xamarin's Developer Modal Pages sample. Your solution does not work, the Custom Tool setting by the way is already set to what you mention.
  • penguru
    penguru over 7 years
    Worked for me as well!
  • Dpedrinha
    Dpedrinha over 7 years
    Actually updating the Xamarin.Forms is the main reason this bug happens. I don't know what they are doing but every stable release this bug comes back. Even after pre releases that fixed it. Each time with a different workaround.
  • Bruno Brito
    Bruno Brito about 7 years
    In my project, the value was MSBuild:UpdateDesignTimeXaml. After i change to MSBuild:Compile its fine. VS SUCKS
  • Dushyant Bangal
    Dushyant Bangal about 7 years
    @Veverke, sorry for the late reply, but look at the bottom of my answer. If you already have MSBuild:Compile just open the Xaml and code behind files and save them. I know, its not the best solution, but it gets the job done.
  • Diego Rafael Souza
    Diego Rafael Souza about 7 years
    Recently I had same issue, and this solution was not solving because of the very long path of my project files. I mean more then 160 chars 'til the class name. The .g.cs was unable to be generated. Changing the path ended the struggle.
  • shalin
    shalin almost 7 years
    This answer worked, unload error containing proj and edit .csproj file to have xamarin version you have in package folder
  • Cobus Kruger
    Cobus Kruger over 6 years
    Fun fact: If you use the rename refactor on the class, the XAML is not updated, leading to this error. The IDE, as ever, is no help.
  • rob_james
    rob_james about 6 years
    Yes, I found the same. Nothing worked. Eventually, copying the content, completely deleting the files and re-adding them from scratch was the only solution.
  • MrClan
    MrClan almost 6 years
    yes, this is the major reason. Whenever we change namespace or class name of xaml.cs file, the associated xaml file is not updated. And this results in the mentioned error.
  • Konstantin
    Konstantin over 5 years
    I didn't change the encoding. Simple "rebuild" helped in my situation
  • cagri
    cagri over 5 years
    i had 1 error. now i have 2 errors after doing these steps.
  • HASSAN MD TAREQ
    HASSAN MD TAREQ over 5 years
    Worked. I renamed Foo.xaml.cs but forgot to change x:Class="XXX.Foo" (refatoring is bad in VS)
  • Vering
    Vering over 5 years
    THANK YOU!!! Been running around for hours with no working solution. It did the same thing - renamed the xaml.
  • Paul McCarthy
    Paul McCarthy almost 5 years
    "Lets auto generate code and have no link in the project for where it is or came from." What fu@!ing idition thought that this was a good idea.
  • Ryano
    Ryano about 2 years
    Worked for me too. Just had to make sure the x:class matched the real class. Thank you!