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

前端 未结 30 2283
不知归路
不知归路 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 .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.

提交回复
热议问题