visual-studio 在 Visual Studio 中对文件进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3617539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:24:29  来源:igfitidea点击:

Group files in Visual Studio

visual-studio

提问by mat-mcloughlin

I'm looking at tidying up my project layout in Visual Studio and I'm wondering if there is any hack, plugin or trick to associate an .xml file with a .cs file of the same nameso they appear grouped in my solution navigator/explorer.

我正在考虑在 Visual Studio 中整理我的项目布局,我想知道是否有任何技巧、插件或技巧可以将 .xml 文件与同名的 .cs 文件相关联,以便它们在我的解决方案导航器中分组出现/探险家。

Similar to the way the code-behind file is associated with its aspx.

类似于代码隐藏文件与其 aspx 关联的方式。

alt text

替代文字

Any suggestions welcome. Thanks

欢迎任何建议。谢谢

采纳答案by Julien Hoarau

In your project file :

在您的项目文件中:

<Compile Include="FileA.cs"/>
<Compile Include="FileA.xml">
  <DependentUpon>FileA.cs</DependentUpon>
</Compile>

Or you could use Group Itemscommand of VSCommands 2010extension.

或者您可以使用VSCommands 2010扩展的Group Items命令。

Edit: Just in case your file is in a folder, don't include the folder name in DependentUpontag. For example if your file is in Helpersfolder:

编辑:以防万一您的文件在文件夹中,请不要在DependentUpon标签中包含文件夹名称。例如,如果您的文件在Helpers文件夹中:

<Compile Include="Helpers\FileA.cs"/>
<Compile Include="Helpers\FileA.xml">
  <DependentUpon>FileA.cs</DependentUpon>
</Compile>

回答by Dao

If you do not want to slow down you IDE with heavy and proprietary VSCommands extension you can use small extension NestIninstead. It can nothing but group/ungroup files

如果您不想使用繁重的专有 VSCommands 扩展减慢 IDE 的速度,则可以改用小型扩展NestIn。它只能对文件进行分组/取消分组

回答by Veldaeven

For the simple case where the file is a "top level" file, Julien's description works perfectly. However, in the case where the DependentUpon file is in a Folder under the project, this looks different. I personally don't like it because it seems like it could lead to ambiguity, but that's an opinion.

对于文件是“顶级”文件的简单情况,Julien 的描述非常有效。但是,在 DependentUpon 文件位于项目下的文件夹中的情况下,这看起来有所不同。我个人不喜欢它,因为它看起来可能会导致歧义,但这是一种意见。

<Compile Include="DataStructs\CKDTree.cs" />
<Compile Include="DataStructs\CClosestObjects.cs" >
    <DependentUpon>CKDTree.cs</DependentUpon>
</Compile>

Notice that the dependent item does NOT include the Folder of the parent. This is true in VS2013... probably true in earlier versions but I have not verified it.

请注意,依赖项不包括父项的文件夹。这在 VS2013 中是正确的……在早期版本中可能是正确的,但我还没有验证过。

回答by Tohid

File Nestingextension for visual studio is a good one. It has about 500K downloads at the time of writing this answer. I added it personally to my VS 2015 and it was working fine (haven't tried it with VS 2017 yet).

Visual Studio 的文件嵌套扩展是一个很好的扩展。在撰写此答案时,它的下载量约为 500K。我亲自将它添加到我的 VS 2015 中,它运行良好(还没有在 VS 2017 中尝试过)。

回答by Mark A. Donohoe

Not sure if people are aware, but nesting files like this seemingly breaks VS's ability to rename the root file, at least when your new nested file is also a partial class. For instance here's the tree we created...

不确定人们是否知道,但像这样的嵌套文件似乎破坏了 VS 重命名根文件的能力,至少当您的新嵌套文件也是部分类时。例如,这是我们创建的树...

MainWindow.xaml
    MainWindow.xaml.cs
    MainWindow.Commands.cs

MainWindow.Commands.cs is just another partial class of MainWindow, same as MainWindow.xaml.cs. However, if you then try and rename MainWindow.xaml, instead of automatically renaming the dependent files, it throws an exception.

MainWindow.Commands.cs 只是 MainWindow 的另一个部分类,与 MainWindow.xaml.cs 相同。但是,如果您随后尝试重命名 MainWindow.xaml,它不会自动重命名依赖文件,而是会引发异常。

For completeness, I also tried naming the file MainWindow.xaml.Commands.csbut that didn't work either.

为了完整起见,我也尝试命名文件,MainWindow.xaml.Commands.cs但这也不起作用。

Without the extra 'commands' file, rename works fine, of course.

如果没有额外的“命令”文件,重命名当然可以正常工作。

MainWindow.xaml
    MainWindow.xaml.cs

Anyway, this was reason enough for us to abandon nesting files like this. Without the ability to rename, it's just not worth it.

无论如何,这足以让我们放弃这样的嵌套文件。如果没有重命名的能力,那就不值得了。