C# 在 Visual Studio 中隐藏区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/858132/
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-08-05 04:28:52  来源:igfitidea点击:

Hiding the regions in Visual Studio

c#visual-studio-2008regions

提问by KevinT

I know VS code folding issues are an old chestnut, but I haven't been able to find this in all the other discussions I have browsed through:

我知道 VS 代码折叠问题是老生常谈了,但是我在浏览过的所有其他讨论中都找不到这个问题:

We have a team of C# guys, some love regions and others hate them and we don't seem to have much middle ground to work with.

我们有一个由 C# 人员组成的团队,有些人喜欢区域,有些人讨厌他们,我们似乎没有太多中间立场可以合作。

Is there a plug- or add-in for VS that will just 'hide' the regions? So that those that want them will see them as normal, but the people that install the add-in and view a .cs file the regions just aren't there, as if they don't exist.

是否有用于 VS 的插件或插件可以“隐藏”区域?这样那些需要它们的人就会像平常一样看到它们,但是安装加载项和查看 .cs 文件的人只是不存在这些区域,就好像它们不存在一样。

I can see this might be an issue when moving code around that it might cause issues of certain methods being in or outside of the wrong region, but that might be a tradeoff the team is happy with...

我可以看到在移动代码时这可能是一个问题,它可能会导致某些方法在错误区域内或外的问题,但这可能是团队满意的权衡......

采纳答案by NotDan

I hate regions (my team loves them) and was surprised to find that nobody has written an extension to make them better. I finally wrote one myself called I Hate #Regions:

我讨厌区域(我的团队喜欢它们)并且惊讶地发现没有人编写扩展来使它们变得更好。我终于自己写了一篇名为I Hate #Regions 的文章

Make #regions suck less (for free):

让 #regions 少吸(免费):

http://visualstudiogallery.msdn.microsoft.com/0ca60d35-1e02-43b7-bf59-ac7deb9afbca

http://visualstudiogallery.msdn.microsoft.com/0ca60d35-1e02-43b7-bf59-ac7deb9afbca

  • Auto Expand regions when a file is opened
  • Optionally prevent regions from being collapsed (but still be able to collapse other code)
  • Give the #region / #end region lines a smaller, lighter background so they are less noticeable (also an option)
  • Works in C# and VB
  • 打开文件时自动扩展区域
  • 可选地防止区域被折叠(但仍然能够折叠其他代码)
  • 给 #region / #end 区域线一个更小、更亮的背景,这样它们就不那么明显了(也是一个选项)
  • 在 C# 和 VB 中工作

Region Tool Screenshot

区域工具截图

回答by Mike Caron

I don't know of any plugin like that, honestly. However, with VSTO, it's very easy to write one yourself.

老实说,我不知道有任何这样的插件。但是,使用VSTO,自己编写一个非常容易。

回答by x0n

Personally, I write a VS macro (Tools > Macro) to expand all regions on file open.

就个人而言,我编写了一个 VS 宏(工具 > 宏)来扩展打开文件的所有区域。

回答by BrianH

This is somewhat... sleazy, and may have side-effects but:

这有点……低俗,可能有副作用,但是:

Tools-->Options-->Environment-->Fonts and Colors-->Preprocessor Keyword Change the foreground and background colours to white (or whatever your default background is).

工具-->选项-->环境-->字体和颜色-->预处理器关键字将前景色和背景色更改为白色(或任何您的默认背景)。

You wont see other preprocessor keywords though.

不过,您不会看到其他预处理器关键字。

回答by Justin Ethier

There are shortcut keys to deal with them:

有快捷键可以处理它们:

Ctrl+M, Ctrl+M  Collapse or expand the block you're currently in.
Ctrl+M, Ctrl+O  Collapse all blocks in the file
Ctrl+M, Ctrl+L  Expand all blocks in the file
Ctrl+M, Ctrl+P  Stop outlining mode. (Ctrl+M, Ctrl+O resumes) 

See The Problem With Code Folding

查看代码折叠问题

回答by sscheider

For VS 2008 users, you can stop outlining and restart it programatically (via a macro). I found this code at http://weblogs.asp.net/rweigelt/archive/2003/07/06/9741.aspx

对于 VS 2008 用户,您可以停止概述并以编程方式(通过宏)重新启动它。我在http://weblogs.asp.net/rweigelt/archive/2003/07/06/9741.aspx找到了这个代码

Imports EnvDTE

' Expands all regions in the current document    
Sub ExpandAllRegions()        
    DTE.ExecuteCommand("Edit.StopOutlining")        
    DTE.ExecuteCommand("Edit.StartAutomaticOutlining")    
End Sub

The above works for C# IDE; for some reason, VB's IDE doesn't remove outlining with regions when explicitly told to. Hope this helps!

以上适用于 C# IDE;出于某种原因,VB 的 IDE 在明确告知时不会删除带有区域的轮廓。希望这可以帮助!

-- appended edit:

-- 附加编辑:

Here's one that does work for Visual Basic .NET in Visual Studio 2008. It's a bit of brute force, but the idea is to comment all of the #region markers, expanding the outline. The next step uncomments them, returning them to their original state (not strictly true... could have rogue uses of #region in other parts of the code). This code doesn't return the Find & Replace dialog back to it's original state, either. With those caveats, this is the macro module that will perform this chore:

这是一个在 Visual Studio 2008 中适用于 Visual Basic .NET 的方法。它有点暴力,但其想法是注释所有 #region 标记,扩展大纲。下一步取消它们的注释,将它们返回到它们的原始状态(严格来说并非如此……可能在代码的其他部分中对#region 进行了恶意使用)。此代码也不会将“查找和替换”对话框返回到其原始状态。有了这些警告,这是将执行此杂务的宏模块:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module MacroMod01
    Sub ExpandAllRegions()
    ' comment out all #region occurances
        DTE.ExecuteCommand("Edit.Replace")
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "#region"
        DTE.Find.ReplaceWith = "'#region"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()

    ' uncomment all #region occurances
        DTE.ExecuteCommand("Edit.Replace")
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "'#region"
        DTE.Find.ReplaceWith = "#region"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()

    'close the find 'n replace dialog
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()  

    End Sub

End Module

There may be a few other methods to accomplish this for VB in VS2008. I'll post as I find them.

在 VS2008 中,可能还有其他一些方法可以为 VB 实现这一点。我会在找到它们时发布。

回答by user1054326

I think it is funny anyone that hates Regions. I love Regions so much I wrote a program called Regionizer, which is an open source project located at http://regionizer.codeplex.com

我认为任何讨厌 Region 的人都很有趣。我非常喜欢 Regions 我编写了一个名为 Regionizer 的程序,这是一个位于http://regionizer.codeplex.com 的开源项目

I have had bosses and some team members who hate them, but my tool organizes all code alphabetically, so methods are alphabetically sorted in the Methods Region, Properties are sorted in the Properties Region, and Events are sorted in the Events region.

我有过讨厌他们的老板和一些团队成员,但我的工具按字母顺序组织所有代码,因此方法在方法区域中按字母顺序排序,属性在属性区域中排序,事件在事件区域中排序。

Try to find code without this tool (or Regions) is what I call Spaghetti Code).

尝试在没有此工具(或区域)的情况下查找代码就是我所说的意大利面条代码)。

I have been searching for an easy way to collapse and expand all regions and have been told by MS that the regions are part of the .suo file (same name as the project or solution) and there is not an easy way to get a handle on the regions object them selves.

我一直在寻找一种简单的方法来折叠和展开所有区域,并被 MS 告知这些区域是 .suo 文件(与项目或解决方案同名)的一部分,并且没有一种简单的方法来处理在地区反对他们自己。

I have searched all through the DTE and I haven't found a way to collapse all or Expand all regions, I am going to try the Macro code and see if it works.

我已经在 DTE 中搜索了所有内容,但还没有找到折叠所有区域或展开所有区域的方法,我将尝试使用宏代码,看看它是否有效。

Will post an update shortly.

将很快发布更新。