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

前端 未结 30 2273
不知归路
不知归路 2021-01-30 12:36

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/getti

相关标签:
30条回答
  • 2021-01-30 13:04

    I'm using Visual Studio 2015, I got the same problem, and found none of the answers on the net was a full solution. Here I'll explain what worked for me.

    My primary goal here was to eliminate the red error message that kept coming up

    The name InitializeComponent does not exist in the current context

    Basically I created a function called InitializeComponent2() that does exactly the same thing as InitializeComponent() and used that instead, I literally copied the code for InitializeComponent(). Does the job.

    It looks like this (and I'll explain):

    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
    private void InitializeComponent2()
    {
        // Change to:
        //   this.LoadFromXaml(typeof(Page2)); // for Page2
        //   this.LoadFromXaml(typeof(Page3)); // for Page3
        // and put
        //   using Xamarin.Forms.Xaml;
        // at the top of each source file.
        this.LoadFromXaml(typeof(Page1));
    }
    

    Put the function in the definition of each of your pages (.cs files) e.g.

    public partial class Page1 : ContentPage
    {
        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent2()
        {
            // Change to:
            //   this.LoadFromXaml(typeof(Page2)); // for Page2
            //   this.LoadFromXaml(typeof(Page3)); // for Page3
            // and put 
            //   using Xamarin.Forms.Xaml; 
            // at the top of each source file.
            this.LoadFromXaml(typeof(Page1));
        }
    }
    

    Also you need to put using Xamarin.Forms.Xaml; at the top of each .cs page where LoadFromXaml(..) is used.

    And then of course change InitializeComponent() to InitializeComponent2() to call the new function. i.e. you have put the function into the same context as the page making the error go away. I can't imagine the real InitializeComponent function will get anything added to it as you modify your project but that is a possibility. It's been fine for me so far.

    I tried many things, changing the Build Action, the XAML namespace, restarting vs, cleaning+rebuilding, looking for NuGet package updates.

    Basically, Visual Studio compiled and ran the program fine on my android device + emulator, but that error message wouldn't go away. My solution is for Android, but it may also work for iOS etc.

    I went back to the root of the problem InitializeComponent() The actual code for this function is generated in a file called <Your Project Name>.Page1.xaml.g.cs or Page2.xaml.g.cs for example. However, it (the file) is only generated when a certain event gets fired. I was lucky to discover it by typing text into "Custom Tool Namespace", which fired that event, for one of the xaml pages(a file ending in .xaml, not .cs - make sure you have .xaml file selected), type some text and press enter and the file will be created.

    Then I had the great idea of making InitializeComponent2(), a function exactly the same as InitializeComponent() and putting it in each cs file so it exists without having to generate the .xaml.g.cs every time you want the error to go away.

    0 讨论(0)
  • 2021-01-30 13:05

    Try adding a x:Name="..." on the xaml page... Nothing else worked for me - but after adding the x:Name attribute on some of the elements on the page the error dissapeared (most of the times - I still get it sometimes). I use the latest build (1.5.0.6447) of Xamarin.Forms...

    XAML don't work on shared projects - it only works in portable projects...

    0 讨论(0)
  • 2021-01-30 13:05

    Ultimate solution would be to edit the project file in a text editor and just copy the pattern from another non-conflicting file. This solution worked for me. Usually, you would look for this syntax:

    <Compile Update="Your\Path\YourContentPage.xaml.cs">
      <DependentUpon>YourContentPage.xaml</DependentUpon>
    </Compile>
    
    0 讨论(0)
  • 2021-01-30 13:06

    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.

    0 讨论(0)
  • 2021-01-30 13:07

    Check the class name properly.

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="{AppName}.{ClassName}">
    </ContentPage>
    

    The class name should be a combination of App name and partial class name

    0 讨论(0)
  • 2021-01-30 13:08

    I Just updated All packages, works fine.

    0 讨论(0)
提交回复
热议问题