VS2008 XAML 代码格式化程序
在我的团队中,.xaml 文件中的代码风格目前不太一致。我们检查了 Visual Studio 自动格式化程序,使其将代码格式化为我们喜欢的格式。然而,对于一个选项,我们缺少一个额外的条件。我正在讨论的选项可以在“工具”->“工具”下找到。选项->文本编辑器 -> XAML->格式化。
我们希望每个属性都在单独的行上。最初,我们还希望第一个属性位于新行(标签开头下方),如下所示:
<MyFooBarButton
Attrib1="a"
Attrib2="b" />
但我们很快意识到,使用这些规则运行将使仅设置一个属性的标签看起来很糟糕,特别是如果它们是嵌套的:
<MyFooBarButton
Attrib1="a" />
<NestedFoo
Attrib="b" />
<NestedFoo2
Attrib="c" />
所以我们尝试将第一个属性放置在与开始标记相同的行上。同时仍然保留两个重要规则(每行一个属性,垂直对齐)。这两种情况看起来都不错:
<MyFooBarButton Attrib1="a"
Attrib2="b" />
<MyFooBarButton Attrib1="a" />
<NestedFoo Attrib="b" />
<NestedFoo2 Attrib="c" />
现在的问题是 Visual Studio 似乎缺乏同时具备这两种情况的条件。即:
- 如果仅设置一个属性:将其保留在一行上。
- 如果设置了多个属性:将第一个属性放在新行上,每行一个属性并将它们垂直对齐。
Visual Studio 2008 可以做到这一点吗?如果没有,ReSharper 中的代码格式化程序可以做到这一点吗(可能值得付出代价)?
In my team, the code style in .xaml files are currently not very consistent. We looked over the Visual Studio auto formatter to make it format the code into something we like. However, for one option we lack an extra condition. The options Im talking about are found under Tools -> Options -> Text Editor -> XAML -> Formatting.
We want each attribute on a separate line. Initially, we also wanted the first attribute on a new line (below the start of the tag) like so:
<MyFooBarButton
Attrib1="a"
Attrib2="b" />
But we quickly realized that running with those rules would make tags that only set one attribute look horrible, especially if the were nested:
<MyFooBarButton
Attrib1="a" />
<NestedFoo
Attrib="b" />
<NestedFoo2
Attrib="c" />
So we tried positioning the first attribute on the same line as the start tag. While still keeping the two important rules (one attribute per line, vertically aligned). It looked decent in both cases:
<MyFooBarButton Attrib1="a"
Attrib2="b" />
<MyFooBarButton Attrib1="a" />
<NestedFoo Attrib="b" />
<NestedFoo2 Attrib="c" />
The problem now is that Visual Studio seems to lack the condition to have both. I.e.:
- If only one attribute is set: keep it on one line.
- If more than one attribute is set: place first attribute on a new line, one attribute per line and align them vertically.
Can Visual Studio 2008 be made to do this? And if not, could the code formatter in ReSharper do it (might make it worth the cost)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 http://xamlstyler.codeplex.com/ ,如果一个元素只有 2 个或少于 2 个属性,这个 xaml 格式化程序不会将属性分成不同的行,此外,这个格式化程序还能够按照预定义的规则对元素的属性进行排序,这使得您的标记的外观要好得多。
Try http://xamlstyler.codeplex.com/, this xaml formatter won't break attributes into different lines, if an element has only 2 or less than 2 attributes, also, this formatter has the capability to sort the attributes of an element in a predefined rule, which make s the look of your markup much better.
也许这些会对您有所帮助:
http://weblogs.asp.net/fmarguerie/archive/2008/06/07/xaml-markup-formatting-in-visual-studio.aspx
链接
http://dimebrain.com/ 2008/05/automating-read.html
(最后两个链接在第一个链接下的文本中提到)
编辑:我检查了 ReSharper 中的选项,但找不到任何仅用于 XAML 格式的选项(有用于 c#、 vb.net 和 XML)
EDIT2:我检查了 VS 中的格式选项,有一些东西似乎正是您所需要的。在“间距”中,有一个选项“将每个属性放置在单独的行上”,并且它具有标题为“将第一个属性放置在与开始标记相同的行上”的复选框。在我看来它应该满足你的需要。
maybe these will help you:
http://weblogs.asp.net/fmarguerie/archive/2008/06/07/xaml-markup-formatting-in-visual-studio.aspx
Link
http://dimebrain.com/2008/05/automating-read.html
(last two links are mentioned in text under first link)
EDIT: I checked options in ReSharper and I couldn't find any options just for XAML formatting (there are for c#, vb.net and XML)
EDIT2: I checked formatting options in VS and there is something that seems exactly what you need. In "Spacing" there is option "Position each attribute on a separate line" and it has checkbox titled "Position first attribute on the same line as start tag". In my opinion it should do what you need.