Add existing xaml files to visual studio 2012
Asked Answered
I

3

20

I want to add a folder containing xaml and cs files plus other directories to a different project on VS. For some reason when I drag the folder from windows explorer to the project where I will like to place that directory visual studio will not let me. Because of that I am manually adding each file and every time I encounter a directory I have to create it. Maybe it is because I am using team foundation server.

Anyways I am adding the files manually so I click on the folder that I want to add the files on visual studio then click on add existing files. Then I select the xaml and code behind file:

enter image description here

when I click add the files get added but visual studio does not recognize that Bytes.xaml.cs is the code behind!

enter image description here

Do I have to manually add a window then copy and paste the contents of the file?

Interceptor answered 14/11, 2012 at 21:20 Comment(0)
C
24

Edit your .csproj file to add a "DependentUpon" element below your "Compile" element for the .xaml.cs file so that it will appear "inside" the .xaml file not simply below it.:

<Compile Include="BytesDisplay\SubControls\Bytes.xaml.cs">
  <DependentUpon>Bytes.xaml</DependentUpon>
</Compile>

To easily edit the .csproj file:

Right-click the project and select "Unload Project"
Right-click the project node again and select "Edit [projectname].csproj"
Edit the XML, then close the file
Right-click the project node again and select "Reload Project"
Cadel answered 15/11, 2012 at 8:59 Comment(0)
H
19

If you drag-and-drop the .xaml file from Windows Explorer into the Solution Explorer window, it will automatically add the .xaml with the code-behind .cs file.

Hidalgo answered 9/12, 2016 at 19:25 Comment(4)
This is so much easier than the accepted answer. If you've already added the files, simply exclude them from the project, and then do the drag and drop from Windows Explorer. Applies to VS 2017 as well.Nakashima
In VS 2017 you need to drag over the xaml and xaml.cs files over togetherBrittne
I tried this but it failed. I discovered that you can't just drag and drop the files into the solution explorer window, you have to drop them right on the project node. VS 2017Gambrel
It's important to drag and drop the xaml file onto the "project name", not in the open space underneath.Jeffjeffcoat
A
0

thanks for the write-up. If you edit the xaml and .cs files class declaration to match without class collisions - then when you add the .xaml file it will pick up the .xaml.cs automatically if it is on the same folder. (vs 2013)

Astrology answered 13/5, 2014 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.