How to add existing XAML file with code behind in Xamarin Studio

I am trying to add existing files to a project. The files are XAML files with a code behind. If I select Add Existing Files it will import them, but they are separated and not nested together like they should be. Is there a way to fix this or import them properly?

Posts

  • DavidSpeakDavidSpeak GBUniversity ✭✭

    Just add the .xaml file (NOT the xaml.cs file - it will follow the .xaml file automagically).

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @Marabunta That was the first thing I tried. It doesn't work though.

  • BoeremakBoeremak NLMember

    I made one in a Phone project and just copied it over. Afterwards I removed all the namespace stuff and added the Xamarin namespaces and called it PlainPage.xaml.

    If I need a new Page I just copy this one and customise from there.

  • DavidSpeakDavidSpeak GBUniversity ✭✭

    @JoshuaNovak

    Sorry just noticed you're using XS not VS - I don't use XS so please ignore my comment :blush:

  • DavidKaplanDavidKaplan USMember

    Shalom!
    Did you find a solution to nesting the cs file to the xaml file?
    Every time I rebuild the project it generates a .g file with
    public partial class UnitStatusPage : global::Xamarin.Forms.AquaBasePage
    and I have to delete the global::Xamarin.Forms. changing it to
    public partial class UnitStatusPage : AquaBasePage

    In the previous Studio version, I would move the cs file on top of the xaml file and it would nest.

    Thanks

  • EdHubbellEdHubbell USMember ✭✭

    Same problem here. Don't really want to go in and edit the .csproj file to make things import correctly, although that seems to be what needs to happen.

  • imcbvimcbv USMember

    I think I found a workaround:

    You need to create a new XAML file and give it the same name as the one you want to import.

    At that point XS will tell you that the file already exists in that folder and ask you if you want to overwrite it or import the existing file.

    Choose "Add existing file" (2 times because XS will do the same for the code behind) and you should have your XAML correctly imported.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    I did find a way to do this, it sucks, but it works. You can copy the files in, but then you have to go in and manually copy the files from your old .csproj to the new one.

  • BrightLeeBrightLee KRMember ✭✭✭

    Thanks @imcbv You saved my time.

  • HarryBlumHarryBlum GBMember ✭✭
    edited February 2017

    @imcbv I used this for ages and it works fine but if you don't like clicking here is another way

    Using a Xamarin PCL Solution:

    1) Go to your PCL folder and open your MySolution.csproj file

    2) There should be several groups of <ItemGroup> tags. One of them declares <EmbeddedResource> tags and another will contain, <Compile> <DependentUpon></DependentUpon></Compile> groups of tags. For MyPage.xaml and MyPage.xaml.cs files to be linked, you must have a group of xmls that declare your xaml page.

    <EmbeddedResource Include="MyPage.xaml">
          <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
          <LogicalName>MyPage.xaml</LogicalName>
    </EmbeddedResource>
    
    <Compile Include="MyPage.xaml.cs">
          <DependentUpon>MyPage.xaml</DependentUpon>
    </Compile>
    

    Note that if your page is in a folder you should specify that like so:

    <Compile Include="Views\MyPage.xaml.cs">
          <DependentUpon>MyPage.xaml</DependentUpon>
    </Compile>
    
    <EmbeddedResource Include="Views\MyPage.xaml">
          <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
          <LogicalName>MyPage.xaml</LogicalName>
    </EmbeddedResource>
    
  • MarioJuniorMarioJunior USMember ✭✭
    edited January 2018

    Hello there

    I've made the procedures described by @imcbv and @HarryBlum, but the problem stills happening.

    I did the "Add Existing Item" described by @imcbv, and the changes in the ".csproj" file, described by @HarryBlum, but my ".xaml.g.cs" file stills appointing to the incorrect classes, like @DavidKaplan had described.

    I've tryed to create an "BeforeBuild" event, and ran a find-and-replace in files, removing the incorrect namespace from the ".xaml.g.cs" files, but when the XAML files are compiled, the wrong namespace is inserted again in code...

    Please, someone can help me with this issue?

    I'm working with Xamarin.Forms v2.3.4.267

  • DavidManeroDavidManero USMember

    @MarioJunior: try cleaning the solution. If that doesn't do it, manually delete the obj and bin folders and then recompile again. That should take care of the issues with the .g.cs files.

  • HarryBlumHarryBlum GBMember ✭✭

    Hi @MarioJunior.

    Have you tried cleaning the build and rebuilding? Otherwise, would you be able to create a git repository for me to have a look at what you've done?

Sign In or Register to comment.