wpf XAML 中的 #region
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2888347/
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
#region in XAML
提问by kenny
I actually don't like #region in my code. BUTfor some reason call me crazy, I would like to have them in my XAML. I would like whole sections to have a #region-like thing and collapse them (e.g. my <Window.CommandBindings>, <Grid.*Definitions>, <Menu>, <Toolbar>, etc..
我实际上不喜欢我的代码中的#region。但是由于某种原因让我发疯,我想在我的 XAML 中使用它们。我希望整个部分都有一个类似 #region 的东西并折叠它们(例如我的 <Window.CommandBindings>、<Grid.*Definitions>、<Menu>、<Toolbar> 等。
Does this exist? If not, how about <RegionCollapse>
这存在吗?如果没有,<RegionCollapse> 怎么样?
回答by dotNET
OP NOTE: this was the original right answerand in some ways still is a good and for some best answer including me. BUT this answers the original question best I think.
OP 注意:这是最初的正确答案,在某些方面仍然是一个很好的答案,对于包括我在内的一些最佳答案。但我认为这最好地回答了最初的问题。
Visual Studio 2015 finally adds support for regions in XAML. You enclose your regionalcode like this:
Visual Studio 2015 最终在 XAML 中添加了对区域的支持。您可以像这样附上您的区域代码:
<!--#region RegionName-->
All Your XAML In Here
<!--#endregion-->
This will add a collapse icon to the left of starting line, using which you can expand/collapse the region.
这将在起始行的左侧添加一个折叠图标,您可以使用它来展开/折叠区域。
Don't know how handy this would practically be since we can already collapse/expand any node in XAML editor. It's there anyway if you want to use.
不知道这实际上有多方便,因为我们已经可以折叠/展开 XAML 编辑器中的任何节点。无论如何,如果您想使用它,它就在那里。
回答by JacobJ
回答by Hans Passant
I might be missing the point of your request, but the XAML editor automatically puts collapsible regions around the elements. Note the nodes in the left rail of the editor window. Click the "-" and the associated element collapses to a single line, much like a #region.
我可能没有抓住您的要求,但 XAML 编辑器会自动在元素周围放置可折叠区域。请注意编辑器窗口左栏中的节点。单击“-”,关联的元素会折叠成一行,很像#region。
回答by Justin Ethier
No, but according to this replyby Microsoft:
不,但根据微软的这个回复:
This is a great suggestion. Thanks very much. We will consider it for a future release
这是一个很好的建议。非常感谢。我们会在未来的版本中考虑它
回答by Clint StLaurent
FYI: This one has been updated to work well with VS 2012 http://visualstudiogallery.msdn.microsoft.com/3c534623-bb05-417f-afc0-c9e26bf0e177
仅供参考:这个已经更新为与 VS 2012 http://visualstudiogallery.msdn.microsoft.com/3c534623-bb05-417f-afc0-c9e26bf0e177
And this VS2012 extension does a nice job of beautifying the XAML so attributes automatically sort and align etc. http://xamlstyler.codeplex.com/documentation
这个 VS2012 扩展在美化 XAML 方面做得很好,因此属性会自动排序和对齐等。 http://xamlstyler.codeplex.com/documentation
回答by razblack
In Visual Studio 2012 Update 4, I was able to select a very large section of XAML code, right click and choose "hide selection". (basically collapses that section with the [...] notation, and after expanding it, the system "remembers" that I wanted to collapse that portion with the [-] notation next to the line numbers.
在 Visual Studio 2012 Update 4 中,我能够选择非常大的 XAML 代码部分,右键单击并选择“隐藏选择”。(基本上用 [...] 符号折叠那个部分,在展开它之后,系统“记住”我想用行号旁边的 [-] 符号折叠那个部分。
回答by Lance Cleveland
You can add comments to the XAML file using SSI formatting:
您可以使用 SSI 格式向 XAML 文件添加注释:
<!-- Grid Styles -->
<style...>
<style...>
<!-- Window Styles -->
<style...>
<style...>
<style...>
This at least gives you some sort of "header" if you use the built-in XML collapse.
如果您使用内置的 XML 折叠,这至少会为您提供某种“标题”。
回答by Scaramanga
Yes,
是的,
VS2010 will remember the collapsed areas but beware this stopped working when I applied SP1 (as did a lot of other things). After uninstalling VS2010 (and any extensions) and doing the following install procedure the XAML elements that were collapsed seemed to be remembered fine:
VS2010 会记住折叠区域,但要注意当我应用 SP1 时它停止工作(就像很多其他事情一样)。在卸载 VS2010(和任何扩展)并执行以下安装过程后,被折叠的 XAML 元素似乎被很好地记住了:
- Install VS2010 (Pro in my case)
- Install VS2010 windows updates (including an XML editor one, 200MB+)
- Install SP1 Install
- Install VS2010 SP1 windows updates
- 安装 VS2010(在我的情况下是 Pro)
- 安装 VS2010 windows 更新(包括一个 XML 编辑器,200MB+)
- 安装 SP1 安装
- 安装 VS2010 SP1 windows 更新
Might help someone.
可能会帮助某人。