Forum Xamarin.Forms

InitializeComponent does not exist

KaoriFuri22KaoriFuri22 USMember ✭✭

Hi, I'm having difficulties on XAML files in a Sheared project.

Using Xamarin Studio;
What I did was simply adding a xaml page to the project and build without adding any code. And it gives me the "The name InitializeComponent does not exist in this context" error (please refer the screen shot).

Hopefully someone could help me..

Also, when I remove a xaml file from the project by clicking "Remove" and select "Delete", and create and add a xaml file with the same name again, it shows multiple .cs files under the xaml file. It's not critical but it's a bit annoying. (see the screen shot too) This happens regardless if it's in Sheared project or not.



  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    Does it still build for you? (the multiple .cs file thing is weird for sure)

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    is this iOS on Xamarin.Studio ? You'd better use PCL instead of Shared Assets Project in this case.

  • KaoriFuri22KaoriFuri22 USMember ✭✭
    edited June 2014

    Thanks for the quick responses,
    Sorry I attached images but they don't show up for some reason. Hopefully this time it goes through.


    Well, it doesn't build because of the InitializeComponent function, but if I comment that out, it does build. If you look in Finder (or file explorer), you will see only one .cs file. But very weird thing is that to make it compiled, I had to comment out the statement as many time as the number of the multiple .cs files..


    Actually I thought it was a Shared project issue, but now it's happening in the iOS project too.. Any xams page with InitializeComponent function doesn't compile anymore.. What can I do???

  • ChrisgozdChrisgozd USMember ✭✭

    I've seen the multiple .cs issue. I usually just right click the top .xaml file and refresh and they go away.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @JasonASmith‌, @StephaneDelcroix‌ - I'm seeing the 'Initialize component does not exist' problem when creating a very simple shared project solution and adding a single XAML. Is this a known bug?

    I'd prefer not to use PCL, as my preference is shared projects!

  • CraigDunnCraigDunn USXamarin Team Xamurai

    For now, the iOS build-tool-chain does not know about Xaml (well, not enough to generate the .g.cs backing codefile with partial class that implements InitializeComponent).

    This means you can't add Xaml files directly to a Xamarin.iOS project... and because Shared Projects work by compiling their contents in the referencing project, by extension you can't currently use Xaml in Shared Projects either (if iOS is one of your targets).

    Xaml works fine in Portable Class Library (PCL) Xamarin.Forms solutions.

  • AlexBlountAlexBlount AUUniversity ✭✭

    Is there an eta on a fix for this?

  • SongtiveSongtive LVMember ✭✭

    Hi @CraigDunn,

    Any plans to resolve that issue in near future?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @songtv, definitely will be fixed, but unfortunately no definite date at this time.

  • SmartyPSmartyP USInsider, University ✭✭
    edited June 2014

    I'm running into this same issue (without the duplicate code behind files). I currently have the beta channel selected (5.1 build 473), but the same issue happens in alpha as well.

    Repro steps in Xamarin Studio on Mac:

    1) Create a new solution (Mobile Apps > Blank App (Xamarin.Forms Portable))

    2) Try to add a new page to the shared solution

    3) Get a compile error of 'InitializeComponent does not exist'

    The only way I found to fix it was to create the solution in VS2013 instead and then use it from XS on the Mac. After that I can add pages in XS that compile fine.

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @SmartyP I can't repro (I'm on alpha XS 5.1.1 build 1). The issue at the start of this thread was a known-issue with Shared Projects, what you are seeing should not happen with PCL! Are you able to diff the working and failing projects to see what might be causing it?

  • SmartyPSmartyP USInsider, University ✭✭

    @CraigDunn‌ Hey Craig! This is with Shared Projects as well, not PCL - specifically a Xamarin Forms shared project on Xamarin Studio on a Mac. I kept getting the 'InitializeComponent does not exist' anytime I tried to add a XAML page and build. I was able to get around it by creating it in VS instead and getting it to compile there before then going to Mac.

    Attached are 2 solutions - one made on Mac, one made on PC. The project structure created by each is a good bit different, so I haven't tried to compare them yet. The Mac one will give the build error if you try to compile it, but the VS one works fine. All I had to do was the 2 steps above - make a new solution, and add a new XAML page to the shared project. Thanks much!

  • CraigDunnCraigDunn USXamarin Team Xamurai
    edited June 2014

    @SmartyP‌ Hey ;) there's a known issue - the combination of:

    • Shared Projects
    • Xaml
    • iOS app
    • Xamarin Studio on OS X

    does not work. The Xamarin Studio on OS X compilation toolchain isn't wired up to produce the .g.cs "code behind" file (which is where InitializeComponent is generated)... and because Shared Projects pull their contents into the referencing project, it fails. In VS on Windows it's fine, so that's why if you move it over afterward the project works. PCL is also fine (on all platforms), because the PCL toolchain knows how to do it - it's just iOS apps on OS X that fail.

    We're working on it! But for now the options are:

    • only use PCL if you want to use Xaml across both IDEs
    • work in Visual Studio if you want to use Xaml with Shared Projects
  • SarthakDattSarthakDatt USMember

    Hey Craig,

    I just created a shared project with VS 2013 update 2 (Have all 3 target platforms).
    I still see this issue when creating a new "Forms Xaml Page".
    Also, I see that in Xamarin Studio I get the option of creating a "Forms Xaml View" but not in VS. Any plans on adding this option? Whats the work around for now?

  • SarthakDattSarthakDatt USMember

    Has anyone tried the same combination- Using Xaml inside a shared code project in VS? Experience issues with that?

  • I have the same problem with a PCL project containing the Xaml page. This project is used by the an iOS project and a Windows Phone project.

    Worked in the beginning, but now I always get the reference failure in the .g.cs file for the page.

    I've tried deleting the page which removes the compilation error. Adding a new page brings it back.

    The project was created in VS 2013 and has only been opened in VS 2013.

  • Solved my issue with the .g.cs file...

    My setup was the following:
    Project A called {Company}.{ProjectName}
    Project B called {Company}.Xamarin.Mvvm

    This caused a problem for the using directives that are nested in the namespace scope of the .g.cs file generated from the xaml file.

  • EdwinKlesmanEdwinKlesman USMember ✭✭

    I had the setup using PCL and it did give me the same warning.
    Turned out to be the different namespace that was causing this:
    I performed a quick copy-paste action from the 'Hello XAML' XAML in the tutorials to get a custom XAML screen up but it - ofcourse - has a different x:Class property value. If that one != the namespace and class of your ContentPage this will also trigger the same error...

    Hope this helps out some of you

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @SarthakDatt‌ there is already the option to add a Forms Xaml Page to a Shared Project (as well as Portable Library projects, of course) in Visual Studio (see screenshot).

    If this does not appear for you, there may have been an issue with your Xamarin installation.

  • DavidNeubauerDavidNeubauer USMember

    I'm in XS OSX.
    I have a Shared Project that implements my data layer using Parse as a backend.
    I have a PCL Project that houses all my Xamarin Forms. I need it to reference my shared data layer. Uh oh...

    The stock PCL Xamarin Forms project will not install the Parse SDK package because of targeting issues, therefore i cannot reference my shared data layer that uses Parse. I cannot put Xamarin Forms into the shared project so I'm stuck right?

    Essentially right now I can either use Parse or Forms but not both.

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @DavidNeubauer‌ well, anything is possible.

    You could use Xamarin.Forms but build Pages in C# rather than Xaml. It's only the Xaml pre-compiler that isn't currently working with Xamarin Studio on OS X; all other Xamarin.Forms features work fine in Shared Projects.

    Or you could use dependency injection to supply a Parse implementation to the PCL (as TodoParse sample does).

  • DavidNeubauerDavidNeubauer USMember

    Yeah I had to do a double take on TodoParse but that definitely turned out to be the answer.

    Thanks Craig!

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Glad you've found a way forward. It's worth noting that we will get the Xaml stuff working in Shared Projects eventually :)

  • MURALMURAL USMember ✭✭
    edited August 2014

    I'm also needing to make this.InitializeComponent() auto-generation work for a Shared Project with XAML files on iOS (Android works fine). Please keep us updated!

  • CraigDunnCraigDunn USXamarin Team Xamurai

    The fix for this is definitely on our roadmap, however it is tied to releases of Xamarin Studio and Xamarin.iOS (not Xamarin.Forms NuGet releases), so for now I'd definitely recommend using the PCL template if you need Xaml support on Mac OS X.

  • MURALMURAL USMember ✭✭

    We can't use PCL cause we need platform-specific lib references.

    This is the hack I'm using to make it work in both iOS and Android:

        #if __IOS__
        private ListView list;
        private void InitializeComponent()
            this.list = this.FindByName<ListView>("list");
  • SarthakDattSarthakDatt USMember


    "there is already the option to add a Forms Xaml Page to a Shared Project (as well as Portable Library projects, of course) in Visual Studio (see screenshot).If this does not appear for you, there may have been an issue with your Xamarin installation."

    I did know that and it works as expected, but I was asking about Forms Xaml ContentView, like I see in Xamarin Studio:

  • BazookasMobilestudioBazookasMobilestudio BEUniversity ✭✭

    Is there any news about the time when this issue will be resolved?

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    It hurts me to say, but you now can have Xamarin.Forms and Xaml works for iOS on XS. On the iOS platform project options, click "use MSBuild build engine".

    Then you'll run into issues referencing your custom controls with a clr-namespace that works on all platforms... that's why we still strongly advice to use PCL, and dependency injection, instead of Shared Assets Projects

  • I found the same issue with a PCL project (Xamarin IDE) ... I had created a views folder under the root project folder and the first view added compiled successfully. The second view caused the error. After changing the x:class name by removing the folder name it compiles happily... e.g. : folder called Views x:Class="myApp.Views.MyListPage errors... changed to x:Class="myApp.MyListPage" compiles. Hope this helps someone out until the issue is fixed.

  • I found the same issue with a PCL project (Xamarin IDE) ... I had created a views folder under the root project folder and the first view added compiled successfully. The second view caused the error. After changing the x:class name by removing the folder name it compiles happily... e.g. : folder called Views x:Class="myApp.Views.MyListPage errors... changed to x:Class="myApp.MyListPage" compiles. Hope this helps someone out until the issue is fixed.

    Thanks, this worked for me.

  • David.6954David.6954 USUniversity ✭✭

    So, when is this getting fixed? It has been about six months since @CraigDunn said this will definitely be fixed. Is there at least a timeline now?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Hey, you can now try using XAML in Shared Projects by enabling MSBuild for the iOS project (only). This is done in the Options window (Build > General section). Tick the option as shown here:


    This is still experimental support, but it will enable XAML to be used in Shared Projects and the correct MSBuild task will be able to generate the InitalizeComponent method in the XAML-code-behind.

    PCL projects remain the preferred approach for sharing code using Xamarin.Forms.

  • David.6954David.6954 USUniversity ✭✭

    Awesome, that works good for my project at least. Are there any important things to watch out for when using this checkbox?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    I'm not aware of any specific things you should look out for, other than just highlighting the MSBuild engine for iOS projects is currently unsupported... if you do hit issues, we'd probably advise either switching to PCL or removing XAML from the Shared Project.

  • David.6954David.6954 USUniversity ✭✭

    Alright, will do. Thanks for the quick responses, much appreciated.

  • UttamShahUttamShah USMember ✭✭

    I figured out how to solve the InitializeComponent does not exist error in my PCL project. I am assuming it will be the same way to resolve in the Shared project. When you add a contentpage, compile the project (rebuild all) before you write any xaml or C# code. I am using Xamarin Studio as my VS2013 wont launch and debug in the android simulator for some reason. I just bought 2 business licenses for Xamarin so that I can build in Visual Studio. There are so many bugs with the so hyped up "Xamarin Forms" that its impossible to develop a decent app quickly.

  • NestorLedonNestorLedon USMember ✭✭

    I had ran into this after renaming a xaml file. Looks like Xamarin's refactoring does not rename related strings inside the file itself.


    Be sure to manually update the class name.

  • RobertStubbsRobertStubbs USMember

    Alot of my issues with InitializeComponent after updating to xamarin.forms 1.3.1 etc went away when i upgraded my visual studio 2013 from update 2 to update 4, compiles fine now...try that for anyone else having issues :)

Sign In or Register to comment.