Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autogenerated XAML.g.cs files are not compilable in Xamarin Forms PCL project

I have a Xamarin forms (I'm using Xamarin Studio 5.7) project with a common PCL containing UI components. I was just using Classes (No XAML designers) to start my project and it works well, compiles, and has a ContentPage with a couple sub pages. I decided I would add a new AboutPage.xaml and AboutPage.cs file and go about using the UI to edit my forms. So, I created my new page via New File... Forms ContentPage XAML..... As I mentioned above, it creates my two files.

AboutPage.cs AboutPage.xaml

Resulting files look like this...

AboutPage.cs

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace IPSND.Xamarin
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage ()
        {
            InitializeComponent ();
        }
    }
}

AboutPage.xaml

<?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="IPSND.Xamarin.AboutPage">
    <ContentPage.Content>
        <StackLayout>
            <Image Id="ImageLogo" Aspect = "AspectFit"></Image>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Now, this looks to be fine and I made sure that my class declaration included the namespace. However, when I compile, the resulting AboutPage.xaml.g.cs file looks like this... Notice how the using statements are included inside the namespace and not at the top of the file. Unfortunately, this is not compilable.

What have I done wrong here?

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18408
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace IPSND.Xamarin {
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;


    public partial class AboutPage : ContentPage {

        private void InitializeComponent() {
            this.LoadFromXaml(typeof(AboutPage));
        }
    }
}

This results in the following errors

D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(19,19): Error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'IPSND.Xamarin' (are you missing an assembly reference?) (CS0234) (IPSND.Xamarin)

D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(19,19): Error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'IPSND.Xamarin' (are you missing an assembly reference?) (CS0234) (IPSND.Xamarin)

D:\SVN\IPSND.Xamarin\obj\Debug\AboutPage.xaml.g.cs(38,38): Error CS0246: The type or namespace name 'ContentPage' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (IPSND.Xamarin)

So, then I figured maybe that it was confusing the tail of my namespace (IPSND.Xamarin) with the head of the using for the Xamarin.Forms namespaces... So I changed my namespace on this form set (both the .cs namespace and the XAML class declaration) to be IPSND.Test. This resulted in the same errors unfortunately.

Apparently as Jason pointed out in the comments below, this configuration of having the using statements is not only allowed, but according to this document here, is by design for the generated file. So, I guess the crux of this issue may be more related to my references for Xamarin.Forms in the PCL library (sort of like the error says). Using the document referenced above, I went into my .cs and .xaml files and got them to match exactly (with no using statements at all and no inheritance from ContentPage in the Partial declaration, but that did not help out.... yet.

like image 699
jaskey Avatar asked Feb 07 '15 04:02

jaskey


1 Answers

Okay, credit to Jason..... he was on the right track and nailed it.... My namespace of IPSND.Xamarin was the issue. Just changing the namespace on that specific xaml/cs was not the issue, it was with my project namespace having 'Xamarin' on the end. I made a copy of my entire project and moved everything into IPSND.X and everything compiled fine.

like image 68
jaskey Avatar answered Nov 09 '22 01:11

jaskey