In Visual Studio (2008) is there a way to have a custom dependent file on another custom file?

∥☆過路亽.° 提交于 2019-11-27 08:06:14

Absolutely - but you'll have to edit the project file by hand. Find your "child" file's entry, and change it to be a non-self-closing element, and add a child element to say what it depends on. Here's an example for a .cs file being compiled:

<Compile Include="AssertCount.cs">
  <DependentUpon>MoreEnumerable.cs</DependentUpon>
</Compile>

And here's a version I've just mocked up for the files you've specified - although I expect Home.spark will have an action associated with it rather than "None":

<ItemGroup>
  <Content Include="Home.spark.js">
    <DependentUpon>Home.spark</DependentUpon>
  </Content>
</ItemGroup>
<ItemGroup>
  <None Include="Home.spark" />
</ItemGroup>

Visual Studio displays it just as you'd expect it to.

andrey.tsykunov

There is VSCommands add-in which allow you to configure dependant files directly from IDE

Link updated, previous was http://mokosh.co.uk/vscommands

FYI: When VSCommands is installed, just select all the files you want to be Dependant and the root file, then Right Click -> Group Items... and VSCommands will ask which of the files you would like to be the root file.

I don't know how you can do it via the Visual Studio UI itself, but if you open the csproj file in a text editor, you can see how they do it for Global.asax.

It usually looks something like this:

<Compile Include="Global.asax" />
<Compile Include="Global.asax.cs">
    <DependentUpon>Global.asax</DependentUpon>
</Compile>

Whilst the other solutions here (editing the project file) work for individual files, we've done the same thing, but want it to work automagically for all *.conv and *.conv.js files rather than having to edit the project file for each one.

When we add a new .conv file to our app, if we have a *.conv.js file it automatically nests under it as you'd expect with .aspx and *.aspx.cs.

I can't find the original tutorial I used to set this up (it does involved registry hacking) but here is one I found from a Google search just now that describes the same thing:

http://blog.dotnetwise.com/2009/09/visual-studio-2008-custom-nested-files.html

If you need to add code-behind for some file there is a workaround. For instance you want to make Test.xaml.cs a child of Test.xaml. Complete the following steps:

  1. Exclude both Test.xaml and Test.xaml.cs from project, save project.

  2. Unload project.

  3. Reload project.

  4. Click "Show hidden files" in File Explorer pane.

  5. File Test.xaml.cs will be displayed as child of Test.xaml.

  6. Add files back to project, save project.

That's it. Now Test.xaml.cs is dependent on Test.xaml.

You can edit by hand the project file, has John Skeed suggested, or you can also have a look on the File Nesting Extension for Visual Studio that will do the job for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!