Xamarin Forms + VS2017 + NETStandard: XAML and code behind are not grouped

DonBoxDonBox USMember ✭✭
edited July 2017 in Xamarin.Forms

I added two pages by "Add\New Item" and choosing "Content Page".

The XAML and code behind are not grouped, the .cs should depend on the XAML.

Is this a known issue in VS 2017? I spent a lot of time updating tooling only to discover this isn't working.... I hope it's a problem on my local installation.

Can someone please confirm?

Also, is there a list of known issues related to NETStandard support for Xamarin Forms in VS2017?

Best Answer

  • AdamMeaneyAdamMeaney US ✭✭✭✭✭
    edited July 2017 Accepted Answer

    I used this blog to move over to the new CSPROJ version of .NETStandard.

    Worked well. In the post, I believe in the first .csproj shown, there is this code:

    <ItemGroup>
        <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
        <None Remove = "**\*.xaml" />
    
        < Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
        <EmbeddedResource Include = "**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" />
    </ItemGroup>
    

    This auto grouped all my .xaml and .xaml.cs for the forseeable future.

    As a note to anyone who uses the blog, Resharper XAML Auto-Complete is broken until 2017.2 EAP 6 or above for people who have done this.

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @DonBox ,

    No not an issue with VS 2017 :smile:

    If you can just check the toggle button next to your Home icon in solution explorer,
    I guess with this your problem gets sorted out.

    Hope this helps.

    Regards,
    N Baua

  • DonBoxDonBox USMember ✭✭

    @N_Baua is that a PCL or a NETStandard library?

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @DonBox,

    This is my PCL project view, however the option applies regardless to the types of project,
    This is a visual studio feature to group the XAML (or ASP.NET ) files to their respective code-behind (.CS) files.

    Hope this helps.

    Regards,
    N Baua

  • DonBoxDonBox USMember ✭✭
    edited July 2017

    @N_Baua The issue is with NETStandard library in VS2017, not with PCL. And it has nothing to do with that option I think.

    Try it, create a NETStandard library, and try add a Page. Clicking on the button you mentioned doesn't do anything.

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭

    I've migrated my solution to NETStandard 1.4, against VS2017, and XAML with XAML.CS are grouping correctly for me.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Okey,

    What you suggest makes sense, this must have been not set straight (as I understand that using .net standard in xamarin is still not that popular yet, I may be wrong). However there is a Hack which you can use if you like

    CAUTION: Before you make changes, please backup your project files.

    • Close your project and open your project file i.e YourProjectName.csproj
      You should see something like
      <ItemGroup>
      <Compile Update="Page1.xaml.cs">
      <SubType>Code</SubType>
      </Compile>
      </ItemGroup>

    Change this to

    <ItemGroup>
    <Compile Update="Page1.xaml.cs">
    <DependentUpon>Page1.xaml</DependentUpon>
    <SubType>Code</SubType>
    </Compile>
    </ItemGroup>

    This needs to be done for all the files you wish to nest with the xaml/cs

    • Close the csproj file and re-open your solution again

    Your solution now looks like

    Hope this helps.

    Regards,
    N Baua

  • DonBoxDonBox USMember ✭✭

    @NamyslawSzymaniuk Thanks. What happens if you add a new Page. Does the XAML and .cs group?

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Nope, since this is a hack, This will not automate,
    You need to manually edit the .csproj file.

    (Or create the visual studio actions as I read somewhere, however I do not have experience writing those.)

    Regards,
    N Baua

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭

    Yes, I've just added completelly new XAML page (with XAML.CS associated by default), and XAML.CS is grouped by default, below XAML file in solution explorer.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @NamyslawSzymaniuk ,

    Oh great, I guess someone at NETStandard 1.4 heard us posting this on forum :smiley:

    Kudos man.

    Regards,
    N Baua

  • DonBoxDonBox USMember ✭✭
    edited July 2017

    @NamyslawSzymaniuk Wow, Really? This drives me nuts!

    I'm thinking this could be because you migrated from PCL, you didn't create a NETStandard from scratch.

    Can you please add a new NETSTandard Library to your solution, add Xamarin Forms, and then add a new Page. I really don't think it's going to work.

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭
    edited July 2017

    Well, I did migrate whole my PCL solution to NETStandard (retargetting PCL to NETStandard in project properties), but because of some other issues, it wasn't work at all - app didn't built, there were lot of stupid error, I spend whole weekend for that. But I don't remember, if there were any issues with not linked xaml and xaml.cs

    Then I used this solution https://github.com/adamped/XamarinForms.NetStandard to "manually migrate" whole my app, based on this github repo, and currently I'm working based on that.

    I'll try to add new NETStandard subproject, and test there, if it will work, but at evening.

  • DonBoxDonBox USMember ✭✭

    @NamyslawSzymaniuk Thanks a lot! I preferred to create solution and NETStandard libray in VS2017 from scratch. It's a lot of work, but it's doable. However, I stumbled on this issue with grouping. If you can check later and let me know would be great. Thanks!

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭

    I also prefer to do everthing from scratch, to avoid redundant code/configs etc., but targetting XF project to NETStandard is nightmare :smiley: .
    And try to target to 1.4. I spent really to much weekends and nights, trying to target to 1.6/1.7...

  • DonBoxDonBox USMember ✭✭

    @NamyslawSzymaniuk I didn't have issues with that, but I have just got started.
    What I did is this:

    1. Added a NETStandard library. Set NETStandard 1.6 on it.
    2. Edited .csproj and added <PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
    3. Added Microsoft.NETCore.Portable.Compatibility (I only added this because it was suggested, I'm not sure if it's needed or not)

    All this it's from this video: https://channel9.msdn.com/Shows/XamarinShow/Snack-Pack-15-Upgrading-to-XamarinForms-to-NET-Standard

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭

    Adding Microsoft.NETCore.Portable.Compatibility prevents me from building Android app, I remember that.
    https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/

    Note: If you get the below in any of your project.json files
    Microsoft.NETCore.Portable.Compatibility": "1.0.1
    
    This causes issues in Xamarin.Android and you might get various errors like
    
    provides a compile-time reference assembly for mscorlib on MonoAndroid,Version=v6.0, but there is no run-time assembly compatible with win.
    error CS1703: Multiple assemblies with equivalent identity have been imported
    
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    edited July 2017 Accepted Answer

    I used this blog to move over to the new CSPROJ version of .NETStandard.

    Worked well. In the post, I believe in the first .csproj shown, there is this code:

    <ItemGroup>
        <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
        <None Remove = "**\*.xaml" />
    
        < Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
        <EmbeddedResource Include = "**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" />
    </ItemGroup>
    

    This auto grouped all my .xaml and .xaml.cs for the forseeable future.

    As a note to anyone who uses the blog, Resharper XAML Auto-Complete is broken until 2017.2 EAP 6 or above for people who have done this.

  • DonBoxDonBox USMember ✭✭
    edited July 2017

    @AdamMeaney Thank you!!!

    Are you also using NETStandard 1.6? What issues did you encounter?

    VS2017 Enterprise crashes when I try to edit the resx using the editor. It crashes as soon as I leave the value cell. It doesn't crash when setting the key.

    Any issues with compiling the Android app with Microsoft.NETCore.Portable.Compatibility like how @NamyslawSzymaniuk mentioned in the previous comment?

    I don't really understand the reason to use Microsoft.NETCore.Portable.Compatibility

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    edited July 2017

    I know when I first tried it without following this blog, it tried to add that and got complicated. When I followed the instructions in the blog, I ended up without direct references to Microsoft.NETCore.Portable.Compatibility in my Droid and iOS as I didn't need it with the new csproj thing instead of project.json

    If I recall, there was definitely some finagling to do until I got it to work, but I did it a few months ago and can't recall what was happening.

  • DonBoxDonBox USMember ✭✭

    Thanks again.
    According to https://channel9.msdn.com/Shows/XamarinShow/Snack-Pack-15-Upgrading-to-XamarinForms-to-NET-Standard we only need Microsoft.NETCore.Portable.Compatibility added to the library, not to the native projects.

  • cassionandicassionandi USMember ✭✭

    @AdamMeaney said:
    I used this blog to move over to the new CSPROJ version of .NETStandard.

    Worked well. In the post, I believe in the first .csproj shown, there is this code:

    <ItemGroup>
        <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
        <None Remove = "**\*.xaml" />
    
        < Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
        <EmbeddedResource Include = "**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" />
    </ItemGroup>
    

    This auto grouped all my .xaml and .xaml.cs for the forseeable future.

    As a note to anyone who uses the blog, Resharper XAML Auto-Complete is broken until 2017.2 EAP 6 or above for people who have done this.

    Solved the problem for me (.net standard 2.0)

  • DanielStraughnDanielStraughn USUniversity
    edited October 2017

    @cassionandi Does this work on VS for Mac and Windows? I've tried this and it only works for VS for Mac, breaks VS 2017 compiling. Has anyone found a way to make this work on both IDE's? Thanks

  • AzaanDhallaAzaanDhalla USMember

    None of the above solutions work anymore, has anyone found a solution to this?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @AzaanDhalla Maybe explain what doesn't work.. I still use it when converting projects, its pretty straightforward.

    Make a new netstandard csproj, add all the files, add some references.

  • AzaanDhallaAzaanDhalla USMember
    edited October 2017

    @AdamMeaney Thanks for replying. I'm actually already working on a netstandard project but before Xamarin Forms v2.4.0.282 I would add a xaml file and the the add

    <None Remove="MyPage.xaml"/>
    

    to the csproj and the duplicate file would go away. After the Xamarin Forms update that solution doesn't work anymore, nor does any other solution. It's actually gotten worse now I see 3 xaml files and one cs file.

    Any help would be much a appreciated.

    Other pertinent information
    OS: Mac 10.3 (High Sierra)
    VS: v7.2
    .NETStandard: v2.0

  • AzaanDhallaAzaanDhalla USMember
    edited October 2017

    Also both files are listed as Embedded Resources where as previously one was None. Which is why the previous work around doesn't work. And changing

    <None Remove="MyPage.xaml"/>
    

    to

    <EmbeddedResource Remove="MyPage.xaml"/>
    

    doesn't work either. I was able to get it working perfectly even if I unloaded the project. Only if I closed Visual Studio completely and loaded the entire solution, would the problem reappears and in the csproj

    <ItemGroup>
          <Compile Update="App.xaml.cs">
            <DependentUpon>App.xaml</DependentUpon>
          </Compile>
    </ItemGroup>
    

    would change into

    <ItemGroup>
          <Compile Update="App.xaml.cs">
            <DependentUpon>*.xaml</DependentUpon>
          </Compile>
    </ItemGroup>
    

    causing the second or third file to appear. I know it's a bit convoluted but I can't seem to find a workaround. Again thank you.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    If you are on anything before the 2.4.0 update, adding this and taking out references to the xaml or cs from your csproj worked.

      <ItemGroup>
        <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
        <None Remove="**\*.xaml" />
    
        <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
        <EmbeddedResource Include="**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" />
      </ItemGroup>
    

    Worked.

    Otherwise, I believe we had issues when trying to use 2.4 that required removing the none remove line.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    With Xamarin.Forms 2.4 use this:

    <ItemGroup>
        <!-- https://bugzilla.xamarin.com/show_bug.cgi?id=55591 -->
        <None Remove="**\*.xaml" />
    
        <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />``
      </ItemGroup>
    
  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited October 2017

    With .NET Standard 2.0 support, you don't need any of the lines above in the csproj. If you keep it, it actually produces a build error. If you added them before, remove them.

Sign In or Register to comment.