61

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, that is they don't recognize their dependencies (Red area in Fig). Actually it should appear like Window1 Files (Green Area in the image.) How can I achieve this.

Solution Explorer

I am trying my hands on WPF so may be a layman sort of question.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Marshal
  • 6,551
  • 13
  • 55
  • 91

13 Answers13

82

This is simple, try to add in project existing items and select the XAML (not .cs, etc.) files in list of formats. In VS2010 thats helps.

Vimes
  • 10,577
  • 17
  • 66
  • 86
Papa John
  • 3,764
  • 3
  • 26
  • 23
58

If you cannot get the IDE to do it (Papa John's post), then you can do it by editing the project file.

That information is in the .csproj file (which is an XML file -- you can open it in a text editor, or by right-clicking on it, choosing "unload", and then opening it -- choose reload to load it up as a project again).

Here is the information to look for, you would need to add the "DependentUpon" tag.

<Compile Include="TheFile.xaml.cs">
  <DependentUpon>TheFile.xaml</DependentUpon>
</Compile>
JMarsch
  • 21,484
  • 15
  • 77
  • 125
  • Is the project file format depends of IDE? If so there possible many different formates to connect xaml to code. – Papa John Dec 15 '11 at 16:03
  • The screen shot above was from VS 2010 (you can tell by the style of the tree), so I used a snip from a 2010 project. If you need a different version, the easies way to see what you need is to create a little test project with 1 xaml file, and then open up the project file and see what VS did. The files should be pretty similar for VS 2005 and up (they are just MSBuild files). 2003 and the original VS.Net used a different file format. – JMarsch Dec 19 '11 at 21:00
  • I had this problem in Xamarin Studio (OS X) and this fixed it for me. Thanks! – frijj2k Apr 26 '16 at 09:20
  • I had to do this manually as described here. None of the other answers worked for me (VS2017). – Hein Andre Grønnestad Jan 09 '19 at 11:41
28

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:

  1. In Windows File Explorer (outside of Visual Studio), open the folder where the required files are.
  2. Select both files (.xaml and .xaml.cs) together.
  3. Drag it onto your project name in the Solution Explorer.
  4. Its done! :)
Purohit Hitesh
  • 1,018
  • 16
  • 28
  • 2
    This does work and was very useful for me, allowing more control than the answer above. Do not understand the down vote. – jomalden Mar 21 '16 at 14:54
  • 1
    Lol this answer actually supports multiple files unlike the 41 upvotes and accepted answer... – David Oct 26 '16 at 12:09
  • 2
    I used this and it worked in VS 2017 EXCEPT I had to change the File Properties on the XAML file. The Build Action was Page and the Custom Tool was wrong. After changing the Build Action to Embedded Resource and the Custom Tool to UpdateDesignTimeXaml all was good. – Steve Reed Sr Apr 14 '17 at 18:54
  • 1
    This does work. To be clear you are dragging the two files FROM the file explorer INTO Visual Studio and dropping them on the project item. (Your mouse should indicate where you can drop it.) Also, I did need to close VS 2019 and reopen it for the items to be fixed. – George M Ceaser Jr Jan 07 '20 at 16:48
  • In VS2019, I ended up with duplicates, suffixed with ` - copy`, but this was easily fixed. I also had to reset the Custom Tool, like @SteveReedSr – Bob Sammers Sep 21 '20 at 13:00
8

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

Harry
  • 2,965
  • 1
  • 13
  • 19
5

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.

Sam del Rö
  • 354
  • 5
  • 9
3

Using Xamarin Shared Code solution:

1) Go to you project folder after unloading the shared project

2) Find the projitems file and edit that adding the DependentUpon tag as described in other answers above.

3) Save the file

4) Go back to visual studio and you should get a dialog that allows you to reload all or just open the project again.

dracosveen
  • 51
  • 5
3

An even easier and faster solution for Xamarin Forms projects, no need to touch csproj file at all, very quick fix.

Make sure you have Show All Files selected for solution explorer - it may be on by default.

  1. Select all affected files
  2. Right click > Exclude from Project
  3. Select the same files again (should be faded out)
  4. Right Click > Include in Project

They should now all be nested correctly and all the changes necessary to the .csproj file will be done.

You may have an InitializeComponent() does not exist in the current context) error after this. If that's the case, the simple fix is..

  • Select all affected items and change Build Action from Page to Embedded Resource
Bejasc
  • 930
  • 1
  • 8
  • 20
  • Are you using Mac Visual Studio? Can you tell me exactly where you're seeing these options? They don't appear in any of my menus or right-clicks. – Le Mot Juiced May 10 '18 at 15:14
  • 1
    On Windows VS.NET I had to Exclude from project, save project, quit, and then relaunch. Now the .xaml and .xaml.cs were "together" and I could add back to project. – t9mike Sep 01 '21 at 12:54
  • @t9mike almost 4 years later - I'm still needing weird little workarounds like this, and deleting bin and obj folders before just about every build - purely out of habit now! – Bejasc Sep 02 '21 at 01:01
2

I was able to just restart Visual Studio for Mac 2019 v16.2 and it reconnected my .xaml & .cs files that should've been connected.

If that doesn't work, Hitsa's solution worked for me as well on a separate occasion.

  • 1
    If the dependency gets removed from the .csproj file per JMarsch's answer, your solution won't work. I don't think so VS has any other way to know that what files are depending on each other apart from that attribute in the csproj files. – Marshal Aug 13 '19 at 14:21
1

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

Kris Coleman
  • 416
  • 3
  • 11
1

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

0

The usual - Close Visual Studio, delete the vs folder in the root folder of your solution and reopen Visual Studio (2019) just worked for me.

Rob L
  • 2,124
  • 3
  • 22
  • 50
0

For VS 2019, you have only to edit .csproj file and eliminate all references to .xaml.cs file es:

<ItemGroup>
    <Folder Include="Models\" />
    <Folder Include="Services\" />
    <Folder Include="ViewModels\" />
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Views\MyPage.xaml">
      <LogicalName>MyPage.xaml</LogicalName>
    </EmbeddedResource>
  </ItemGroup>
Baznr
  • 1
0

I found the problem was when i reloaded a github project from my menu, instead of the actual project file inside that local downloaded repo. Once I selected the .sln file it worked again!

Spinstaz
  • 287
  • 6
  • 12