Here's a really neat trick discovered by my colleague Preet that you can use to show a relationship between files in Visual Studio, just like Microsoft does with partial 'designer' classes. You've probably noticed when you add a new windows form in VS 2005 that two files are created: MyForm.cs and MyForm.designer.cs. The designer file contains all the code that's generated by the form designer and the other file is where you write your user code. Preet showed me that you can do the same thing by modifying the .csproj file (aka the MSBuild file) and add a DependentUpon child element to any item that you want to appear below another. Here's a snippet of a .csproj that has three files, all partial classes of 'Foo'. In the solution explorer Foo.1.cs appears nested below Foo.cs and Foo.1.1.cs appears below Foo.1.cs.
<ItemGroup> <Compile Include="Foo.1.1.cs"> <DependentUpon>Foo.1.cs</DependentUpon> </Compile> <Compile Include="Foo.1.cs"> <DependentUpon>Foo.cs</DependentUpon> </Compile> <Compile Include="Foo.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup>
The only way of doing this at the moment is by editing the .csproj file, but it's pretty cool, especially if you're writing code generators and GAT tools like I have been recently where you might want generated content to be visually related to user written code.
Do you know if the DependentUpon tag has any semantic impact on the build process? Does it only impact the VS display?
ReplyDeleteBest regards,
Joannès
blog.lokad.com
When I make a new < SubType >Code< /SubType > file DependentUpon a form type file, studio insists on rewriting my project file to change the type of the dependency into form as well.
ReplyDeleteDo you by any chance know if this is a bug or if there is some trick to preserving the dependent file type?
you can download free addin to visual studio from here: http://www.kooriyoo.com/VSCommands/Default.aspx
ReplyDeleteand group items from VS IDE without need to add DependentUpon manually to project file.
This should also work with SubTypes, but I placed the new Item behind other dependent files, like the C#-Files.
ReplyDeleteFor Example:
<Page Include="HomePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Compile Include="HomePage.xaml.cs">
<DependentUpon>HomePage.xaml</DependentUpon>
</Compile>
<Page Include="HomePage.MyOwnResources.xaml">
<DependentUpon>HomePage.xaml</DependentUpon>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
someone posted the link to visual studio addin. the link appears to be broken. i don't know if it's same addin, but I found one here http://www.mokosh.co.uk/post/VsCommands-12-Released.aspx
ReplyDeleteUpdated link to the tool:
ReplyDeletehttp://mokosh.co.uk/vscommands/
One thing that the original article doesn't point out is that you shouldn't include the path of the "parent" item on the child item. For example, if my object BaseEntity was in Entities\BaseEntity.cs, you'd do:
ReplyDeleteDependentUpon BaseEntity.cs /DependentUpon
Notice that I don't put Entities\BaseEntity.cs in my DependentUpon.
Sorry the brackets aren't coming through because of HTML formatting.