The name 'InitializeComponent' does not exist in the current context

TapasBeheraTapasBehera USMember ✭✭

This is really strange and inconsistent. Sometimes it runs fine and sometimes I get the following error

'The name 'InitializeComponent' does not exist in the current context'

There is absolutely no change in code and project settings. Just sometimes it runs and sometimes it throws this compilation error. How to resolve this?

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Clean and build anyway. Its a false positive.

  • TapasBeheraTapasBehera USMember ✭✭

    I have tried that many times.It will run once and then it won't run with the error. How to resolve this?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Let's see the code for that. My guess is that there is no inheritance on the class.

    for example:
    public partial class HomePage : GenericPage

  • TapasBeheraTapasBehera USMember ✭✭

    This is how it is inheriting
    public partial class ResultPage : ContentPage

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I would think that should be fine. That's about as vanilla as you can get.

    Reboot the PC?
    Double check the XAML of that page? If there is an issue in the XAML, that gets generated to code when you compile... If that generated code is screwed up then the partial class that is the code-behind would probably get squirrely.

  • TapasBeheraTapasBehera USMember ✭✭

    tried all those options.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Don't know what to tell ya in that case. Its not systemic with Xamarin in general or everyone would be seeing the same thing.

  • TapasBeheraTapasBehera USMember ✭✭

    @ClintStLaurent thanks. I hope Xamarin team gives some clear instruction.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Clear instruction... For what? You've got some bad code or markup someplace. Its a problem specific either to your installation or your solution: Not to everyone. So there's little anyone can tell you based on "I get an error".

    Start with a new solution. Make NO CHANGES. Build it 10 times. Does it still happen?
    Make a minor change to one line. Something that can't be screwed up like going from int x = 10; to int x = 20; just to force a re-compile. Build and run. Does it still fail. Do that same kind of minor change 10 times. Do any of them fail?

  • VenkataSwamyVenkataSwamy INMember ✭✭✭
    Hii @TapasBehera

    May be your files BuildActions changed

    Please check your XAML file build action it should be EmbeddedResource

    And c# file build action should be compile

    Right click on files ----> properties then check the build action for both files

    Thank you
  • TapasBeheraTapasBehera USMember ✭✭

    Xamarin studio should tell clear error message. It is giving some error message that cannot be understood.Xamarin team needs to work on this.There is no bad code.It is simple and straightforward code.Even if there is an error then the compiler should tell what is the error as it happens in XCode or Android Studio.But these errors are meaningless and doesn't give any clue where the actual problem is.Team Xamarin needs to improve a lot.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Xamarin studio should {...}

    You can stop right there. Xamarin Studio is end of life. Its out of date at this point. You probably want to update to Visual Studio. Otherwise you have no way to know if any problems you encounter are because of Xamarin Studio being out of date, or if its your code.

  • TapasBeheraTapasBehera USMember ✭✭

    @ClintStLaurent Is there any official news that Xamarin Studio will be phased out?Please share with me.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited May 2017

    Yeah - Months ago. At the Visual Studio 2017 release it was said in one of the presentations that Xamarin Studio wouldn't be receiving additional updates. A commentator interviewing some higher-up or another asked it point blank and got an equally direct answer.

    We all kind of knew it would happen at one point or another. Its just good business sense. Why would a company continue to develop a second IDE when they already own Visual Studio? There is no reason to. Certainly no financial responsibility in doing so.

  • dpedrinhadpedrinha DEMember ✭✭✭

    @ClintStLaurent said:

    Xamarin studio should {...}

    You can stop right there. Xamarin Studio is end of life. Its out of date at this point. You probably want to update to Visual Studio. Otherwise you have no way to know if any problems you encounter are because of Xamarin Studio being out of date, or if its your code.

    Yeah, update to Visual Studio, and as in any other update in this platform, get newer bugs.

    One thing I learned is that one does not update anything in Xamarin unless necessary or you will get more problems.

  • TapasBeheraTapasBehera USMember ✭✭

    anyone from Xamarin Support team please answer why am I getting this error.

  • dpedrinhadpedrinha DEMember ✭✭✭

    @TapasBehera said:
    anyone from Xamarin Support team please answer why am I getting this error.

    Don't wait for it. You can fill a bug report, but I don't think you get any help there either.
    I suggest you create a new project and copy everything little by little until you fix the problem. This error can mean basically anything.

  • zahikramerzahikramer ILMember ✭✭✭

    I have the same problem.
    Tried to changing build action.
    In the past it worked.
    Now not!

  • BrianRunckBrianRunck USMember ✭✭

    I had this problem in Visual Studio 2017 too; it seemed to be an Intellisense-related problem. An actual build worked, and in the Error List window, if I selected "Build - Intellisense" or "Intellisense Only" it displayed the errors, but if I selected "Build Only" the errors went away in the Error List window.
    I fixed it by following the steps MarioLpez suggested above; thank you for that.

  • zahikramerzahikramer ILMember ✭✭✭

    Finally I found the problem of me.
    The problem was XF versioning issue, an old dependency on referenced PCL (netstandard) needed old XF version
    therefore old XF was pulled as a package , therefore the error on "'InitializeComponent' does not exist ".
    The solution was to ReReference in the final project the new XF version (latest) thus activating the "nearest-wins" rule as here: https://docs.microsoft.com/en-us/nuget/consume-packages/dependency-resolution

  • mrquakemrquake BRMember

    @MarioLpez said:
    This is how I fixed it: Clean your project, Rebuild it, change your XAML file properties (Build Action) from Embedded Resource to Compile, Rebuild your project (will throw errors), switch back your XAML file properties to Embedded Resource, Rebuild.

    That worked for me!

  • Thanks that worked for me, too!

  • prishahprishah CAMember ✭✭

    @VenkataSwamy said:
    Hii @TapasBehera

    May be your files BuildActions changed

    Please check your XAML file build action it should be EmbeddedResource

    And c# file build action should be compile

    Right click on files ----> properties then check the build action for both files

    Thank you

    @VenkataSwamy, thanks for the correct input , it has solve my issue.

  • tasytasy USMember

    @MarioLpez said:
    This is how I fixed it: Clean your project, Rebuild it, change your XAML file properties (Build Action) from Embedded Resource to Compile, Rebuild your project (will throw errors), switch back your XAML file properties to Embedded Resource, Rebuild.

    Yeah that's worked for me :smile: Thank you.

  • xProjectsxProjects USMember ✭✭

    Hi.
    Tried the MarioLpez approach, this didi not work entirely. Next, tried, prishah's steps. Still no joy. So I then tried this one. Bingo!
    Everything builds and I can run the project in the Simulator (unrelated, but just in case, I was using jamesmontemagno's IsRefreshing app from github).

  • IbrahimRagabIbrahimRagab Member ✭✭

    This problem happened to me when I bind "IsEnabled" two times by mistake for "switch" like this:

  • MuhammadhamzaMuhammadhamza USMember ✭✭

    Change ContentPage properties to :

    BuildAction => Embedded resource
    CustomTools => MSBuild:UpdateDesignTimeXaml

  • SteveSykesSteveSykes USMember ✭✭

    I was attempting to see what upgrading to the newest forms would do to my project...as always it was a disaster so I opted to restore from TFS and this problem happened. It's clearly not a code issue as I'm restoring every bit of code to a known working condition. MarioLpez's approach resolved my issue. I still have no idea what the real problem was, which is par for the course with Xamarin.

  • AhmedovAhmedovAhmedovAhmedov USMember ✭✭

    I just came to this and the problem was that after renaming the .cs file, I forgot to change the Class in the .xaml file.

  • imurvaiimurvai Member

    Hi, I managed to fix the same issue by these steps:

    • Clean solution
    • Close VS
    • Delete bin and obj folders in the failing project folder
    • Restart VS and go
  • the problem is x:Class=Solution.Name have to be the same to class name.

  • slink99slink99 Member ✭✭

    Thanks @carloslzp I had changed the page name but the x:class was still Page1

  • nick5454nick5454 USUniversity ✭✭✭

    @TapasBehera said:
    anyone from Xamarin Support team please answer why am I getting this error.

    This is always because of your xaml. Usually namespace declaration in the xaml.

  • shaunpryszlakshaunpryszlak USMember ✭✭

    I have this. I know it is something to do with the .cs and .xaml files having something wrong between them as i have fixed it in the past.

    As it is one of those errors with a number of different causes, could the team do something to break it down a bit? Which keyword or token doesn't it like?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'm pretty sure the same process that I outline here still works:

    • Clean the new solution.
    • Close the solution
    • Open the solution in order to have it go through its (Loading...) process.
    • Build each solution individually from the top, downward:
        1. Binding libs (if any)
        1. PCL
        1. Android
        1. iOS
        1. UWP>

      @shaunpryszlak said:

      I have this. I know it is something to do with the .cs and .xaml files having something wrong between them

      Unless you are doing something really weird to make your UI this shouldn't be the case. The XAML and xaml.cs files are made together and match. So if they're out of sync its something you did specifically like a rename of one but not the other. Sometimes global find & replace can bite you.

    If that's not the case... If you didn't do something to throw them out of sync and screw up their naming try the standard clean/close/open/build process above.

  • dugguduggu USMember ✭✭

    Today I have also got this. No clean-up worked for me (Delete Bin/obj etc).

    The issue was with my Xaml where x:Class was pointing to wrong namespace. Failing to this, it throws the above mentioned exception (InitializeComponent doesn't exist). This usually happens if you're doing copy-paste of Xaml markup between solutions and it doesn't throw the right exception. Hope it helps to someone.

Sign In or Register to comment.