I had a VB projected and converted it to C# using online conversion tools. Now the problem is xaml
and xaml.cs
file do not connect to each other, t
Based on Kyle White's comment on the official Xamarin bug report 55591: .xaml files in .NETStandard library appear twice in solution explorer, I found a simple solution to this problem within the linked .NET Standard sample project by Oren Novotny
Within your .csproj file, add the following <ItemGroup>:
<ItemGroup>
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
<EmbeddedResource Include="**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" LogicalName="%(Filename)%(Extension)" />
</ItemGroup>
Afterwards, all xaml
files within the project structure will be displayed in the Solution Explorer window automatically - even if you'll ever add new xaml
files.
How does this magic work?
The Compile element within the ItemGroup is using wildcards to iterate through all directories, searching for .xaml.cs
files and marking them as dependent on the xaml
files of the same name. Please note that this works only because the %(Filename)
item metadata used for the DependentUpon element contains the left-most file extension, which matches the name of the xaml
file by convention.
The EmbeddedResource element will include all xaml
files to the project, so that they are visible within the Solution Explorer window while marking them as Designer files, and declaring that the UpdateDesignTimeXaml
target defined within the Xamarin.Forms NuGet package should be used to generate code from the markup file.
In a Shared Project there is .shproj file instead of a .csproj file and the and items do not exist there. However I found there is also a .projitems file and adding a section there as described above caused the .xaml and .cs files to be linked
If you get the Nested File VS Extension, you can do this by right clicking similarly named files and choosing to "nest automatically" or you can nest manually.
https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
Easiest Way!!!
I came across the same. I got the way out. Here is how to get the .xaml.cs nested under the .xaml in Solution Explorer:
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.
3) 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>
Note that this works with OSX and Windows