[XAML] XAML Styler でコードフォーマットする

2024-12-09 (月)

XAML Styler というXAML のコード整形ツールを使用します。インデント、閉じタグ、属性の並び順までフォーマットすることが可能です。

拡張機能でインストールしたり、NuGet パッケージから dotnet tool として CLI で使用することができます。
XAML Styler for Visual Studio 2022 - Visual Studio Marketplace
NuGet Gallery | XamlStyler.Console

使用方法は単純なので、設定ファイルについて調べてみました。

環境

  • XAML Styler 3.2404.2
  • .NET 9.0.9 (SDK 9.0.101)
  • C# 13.0
  • Visual Studio 2022 Version 17.12.3
  • Windows 11 Pro 23H2 22631.4460

プロパティ一覧

設定できるプロパティを一覧にしました。

Attribute Formatting

JsonPropertyDisplayNameTypeDefault
AttributeIndentationAttribute indentationint0
AttributeIndentationStyleAttribute indentation styleMixed = 0,
Spaces = 1
Spaces
AttributesToleranceAttribute toleranceint2
KeepFirstAttributeOnSameLineKeep first attribute on same lineboolfalse
MaxAttributeCharactersPerLineMax attribute characters per lineint0
MaxAttributesPerLineMax attributes per lineint1
NewlineExemptionElementsNewline exemption elementsstring後述
RemoveDesignTimeReferencesRemove design-time referencesboolfalse
SeparateByGroupsSeparate by groupsboolfalse

Attribute Reordering

JsonPropertyDisplayNameTypeDefault
EnableAttributeReorderingEnable Attribute Reorderingbooltrue
AttributeOrderingRuleGroupsAttribute ordering rule groupsstring[]後述
FirstLineAttributesFirst-line attributesstring""
OrderAttributesByNameOrder attributes by namebooltrue
IgnoreDesignTimeReferencePrefixIgnore Design-Time Reference Prefixboolfalse

Element Formatting

JsonPropertyDisplayNameTypeDefault
PutEndingBracketOnNewLinePut ending brackets on new lineboolfalse
RemoveEndingTagOfEmptyElementRemove ending tag of empty elementsbooltrue
SpaceBeforeClosingSlashSpace before ending slash in self-closing elementsbooltrue
RootElementLineBreakRuleRoot element line breaks between attributesDefault = 0,
Always = 1,
Never = 2
Default

Element Reordering

JsonPropertyDisplayNameTypeDefault
ReorderVSMReorder visual state managerNone = 0,
First = 1,
Last = 2
Last
ReorderGridChildrenReorder grid panel childrenboolfalse
ReorderCanvasChildrenReorder canvas panel childrenboolfalse
ReorderSettersReorder settersNone = 0,
Property = 1,
TargetName = 2,
TargetNameThenProperty = 3
None

Markup Extension Handling

JsonPropertyDisplayNameTypeDefault
FormatMarkupExtensionEnable markup extension formattingbooltrue
NoNewLineMarkupExtensionsKeep markup extensions of these types on one linestringx:Bind, Binding

Miscellaneous

JsonPropertyDisplayNameTypeDefault
FormatOnSaveFormat XAML on Savebooltrue
SaveAndCloseOnFormatAutomatically save and close documents opened by XAML Stylerbooltrue
CommentPaddingComment paddingint2

Thickness Formatting

JsonPropertyDisplayNameTypeDefault
ThicknessSeparatorThickness separatorNone = 0,
Space = 1,
Comma = 2
Comma
ThicknessAttributesThickness attributesstringMargin, Padding, BorderThickness, ThumbnailClipMargin

XAML Styler Configuration

JsonPropertyDisplayNameTypeDefault
SearchToDriveRootSearch to drives rootboolfalse
ResetToDefaultReset to defaultboolfalse
ConfigPathExternal configuration filestring""

xstyler.exe 専用

JsonPropertyTypeDefault
IndentSizeint4
IndentWithTabsbool?false

デフォルト値

ここにデフォルト値の JSON が記載されています。

全てデフォルト値を設定した Settings.XamlStyler ファイルは次の通りです。
上記の JSON を元に作成していますが、一部変更しています (差分は後述)。

{
  "AttributesTolerance": 2,
  "KeepFirstAttributeOnSameLine": false,
  "MaxAttributeCharactersPerLine": 0,
  "MaxAttributesPerLine": 1,
  "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
  "SeparateByGroups": false,
  "AttributeIndentation": 0,
  "AttributeIndentationStyle": 1,
  "RemoveDesignTimeReferences": false,
  "IgnoreDesignTimeReferencePrefix": false,
  "EnableAttributeReordering": true,
  "AttributeOrderingRuleGroups": [
    "x:Class",
    "xmlns, xmlns:x",
    "xmlns:*",
    "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
    "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
    "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
    "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
    "*:*, *",
    "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
    "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
    "Storyboard.*, From, To, Duration"
  ],
  "FirstLineAttributes": "",
  "OrderAttributesByName": true,
  "PutEndingBracketOnNewLine": false,
  "RemoveEndingTagOfEmptyElement": true,
  "SpaceBeforeClosingSlash": true,
  "RootElementLineBreakRule": 0,
  "ReorderVSM": 2,
  "ReorderGridChildren": false,
  "ReorderCanvasChildren": false,
  "ReorderSetters": 0,
  "FormatMarkupExtension": true,
  "NoNewLineMarkupExtensions": "x:Bind, Binding",
  "ThicknessSeparator": 2,
  "ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin",
  "FormatOnSave": true,
  "SaveAndCloseOnFormat": true,
  "CommentPadding": 2,
  "SearchToDriveRoot": false,
  "IndentSize": 4,
  "IndentWithTabs": false
}

Wiki の JSON に対しての変更差分です。設定可能なプロパティを追記しました。

+ "SaveAndCloseOnFormat": true,
+ "SearchToDriveRoot": false,
+ "IndentSize": 4,
+ "IndentWithTabs": false

カスタム設定

好みが分かれそうなプロパティを確認してみます。GitHub で path:**/*.xamlstyler を検索して、適当にピックアップしました。

デフォルト設定に対して変更している設定値の一覧です。

RepoStarAttributesToleranceKeepFirstAttributeOnSameLineAttributeIndentationStyleEnableAttributeReorderingOrderAttributesByNamePutEndingBracketOnNewLineRootElementLineBreakRuleReorderVSMReorderGridChildrenReorderSettersFormatMarkupExtensionThicknessSeparatorCommentPadding
デフォルト2FALSE1TRUETRUEFALSE02FALSE0TRUE22
Uno.Themes1701TRUE0FALSEFALSE01
Uno.Gallery1561TRUE0FALSEFALSE01
Fluent.Ribbon25691TRUE131
snoopwpf22241TRUE1311
Graph-Controls1551TRUE
Material.Avalonia851TRUE
Prism.Avalonia315TRUE
WolvenKit9451FALSE1TRUE31
Lottie-Windows6391TRUE
PSAppDeployToolkit18791FALSE01
MahApps.Metro9345TRUE131
IconPacks.Browser149TRUE131
WindowsCommunityToolkit59041TRUE1
XamlFlair8181TRUE1
audio-band660TRUE
H.NotifyIcon5741FALSETRUE1
Character-Map-UWP6340FALSE0
Sharpnado.MaterialFrame504TRUE
Vulnerator2061TRUETRUE3

個人的には以下のように設定値を変更しようと思います。

- "AttributesTolerance": 2,
+ "AttributesTolerance": 1,

- "KeepFirstAttributeOnSameLine": false,
+ "KeepFirstAttributeOnSameLine": true,

-  "ReorderSetters": 0,
+  "ReorderSetters": 3,

-  "CommentPadding": 2,
+  "CommentPadding": 1,

カスタム順序

以下のプロパティは属性順が定義されています。

  • NewlineExemptionElements
  • AttributeOrderingRuleGroups
  • NoNewLineMarkupExtensions
  • ThicknessAttributes

デフォルト設定に対して変更している差分をピックアップしました。

Uno.Themes/Settings.XamlStyler at master

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, StaticResource",

Fluent.Ribbon/Settings.XAMLStyler at develop

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, MultiTrigger, DataTrigger, MultiDataTrigger, Condition, Setter, SolidColorBrush, Pen",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
+ "x:Key, Key, x:Name, Name, x:Uid, Uid, Title, Header",
+ "TargetType, BasedOn, Style",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
+ "Background, Foreground, FontFamily, FontSize, LineHeight, FontWeight, FontStyle, FontStretch",
+ "SmallIcon, Icon, MediumIcon, LargeIcon",
  "*:*, *",
- "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "PageSource, PageIndex, Color, Opacity, Offset, TargetName, Property, Value, StartPoint, EndPoint",
+ "*:Freeze, mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

- "NoNewLineMarkupExtensions": "x:Bind, Binding",
+ "NoNewLineMarkupExtensions": "x:Bind, Binding, TemplateBinding, x:Static, DynamicResource, ComponentResourceKey, RelativeSource",

Material.Avalonia/Settings.XamlStyler at master · AvaloniaCommunity/Material.Avalonia

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
- "Storyboard.*, From, To, Duration"
+ "Storyboard.*, From, To, Duration",
+ "CommandParameter, Command",
+ "ItemsSource, SelectedItem"
],

snoopwpf/Settings.XAMLStyler at develop · snoopwpf/snoopwpf

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, MultiTrigger, DataTrigger, MultiDataTrigger, Condition, Setter, SolidColorBrush, Pen",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
+ "x:Key, Key, x:Name, Name, x:Uid, Uid, Title, Header",
+ "TargetType, BasedOn, Style",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
- "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "PageSource, PageIndex, Color, Opacity, Offset, TargetName, Property, Value, StartPoint, EndPoint",
+ "*:Freeze, mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

- "NoNewLineMarkupExtensions": "x:Bind, Binding",
+ "NoNewLineMarkupExtensions": "x:Bind, Binding, TemplateBinding, x:Static, DynamicResource, ComponentResourceKey, RelativeSource",

WolvenKit/Settings.XamlStyler at main · WolvenKit/WolvenKit

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, ColumnDefinition, RowDefinition, SolidColorBrush, KeyBinding",

"AttributeOrderingRuleGroups": [
  "x:Class",
+ "x:TypeArguments",
  "xmlns, xmlns:x",
  "xmlns:*",
+ "mc:Ignorable",
+ "d:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
+ "IconPack, Kind",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
+ "Size",
+ "Background, BorderBrush, BorderThickness, CornerRadius",
+ "Foreground, FontSize, FontWeight, FontFamily",
+ "Style",
+ "BasedOn, TargetType",
+ "Visibility, IsEnabled",
+ "HeaderRowHeight, RowHeight, ColumnSizer",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

- "NoNewLineMarkupExtensions": "x:Bind, Binding",
+ "NoNewLineMarkupExtensions": "",

Lottie-Windows/settings.xamlstyler at main · CommunityToolkit/Lottie-Windows

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, ColumnDefinition, RowDefinition, SolidColorBrush, KeyBinding",

"AttributeOrderingRuleGroups": [
  "x:Class",
+ "x:TypeArguments",
  "xmlns, xmlns:x",
  "xmlns:*",
+ "mc:Ignorable",
+ "d:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

PSAppDeployToolkit/src/PSADT.UserInterface/PSADT.Exe/Settings.XamlStyler at main · PSAppDeployToolkit/PSAppDeployToolkit

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "
+   RadialGradientBrush,
+   GradientStop,
+   LinearGradientBrush,
+   ScaleTransfom,
+   SkewTransform,
+   RotateTransform,
+   TranslateTransform,
+   Trigger,
+   EventTrigger,
+   Condition,
+   Setter,
+   Style,
+   SolidColorBrush,
+   ControlTemplate,
+   VisualState,
+   VisualTransition,
+   DoubleAnimationUsingKeyFrames,
+   DiscreteDoubleKeyFrame,
+   LinearDoubleKeyFrame,
+   EasingDoubleKeyFrame,
+   SplineDoubleKeyFrame,
+   ObjectAnimationUsingKeyFrames,
+   DiscreteObjectKeyFrame,
+   BooleanAnimationUsingKeyFrames,
+   DiscreteBooleanKeyFrame,
+   Int32AnimationUsingKeyFrames,
+   DiscreteInt32KeyFrame,
+   ThicknessAnimationUsingKeyFrames,
+   DiscreteThicknessKeyFrame,
+   ScaleTransform,
+   DataTrigger,
+   RowDefinition,
+   ColumnDefinition,
+   primitives:CornerRadiusFilterConverter,
+   ui:CornerRadiusFilterConverter,
+   StaticResource,
+   m:StaticResource,
+   ui:StaticResource,
+   Controls:HamburgerMenuItemStyleSelector,
+   ui:AppBarButton,
+   ui:AppBarToggleButton,
+   Binding,
+   local:RatingItemFontInfo,
+   animation:CubicBezierEase,
+   CommandBinding,
+   ui:VisualStateGroupListener,
+   ImageBrush,
+   ui:BitmapIcon,
+   Storyboard
+ ",

- "NoNewLineMarkupExtensions": "x:Bind, Binding",
+ "NoNewLineMarkupExtensions": "x:Bind, Binding, TemplateBinding",

MahApps.Metro/Settings.XAMLStyler at develop · MahApps/MahApps.Metro

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, MultiTrigger, DataTrigger, MultiDataTrigger, Condition, Setter, SolidColorBrush, Pen",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "*:Freeze, mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

- "NoNewLineMarkupExtensions": "x:Bind, Binding",
+ "NoNewLineMarkupExtensions": "x:Bind, Binding, TemplateBinding, x:Static, DynamicResource, ComponentResourceKey, iconPacks:Modern, iconPacks:Material",

IconPacks.Browser/Settings.XAMLStyler at main · MahApps/IconPacks.Browser

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, MultiTrigger, DataTrigger, MultiDataTrigger, Condition, Setter, SolidColorBrush, Pen",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
+ "x:Key, Key, x:Name, Name, x:Uid, Uid, Title, Header",
+ "TargetType, BasedOn, Style",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
+ "Background, Foreground, FontFamily, FontSize, LineHeight, FontWeight, FontStyle, FontStretch",
  "*:*, *",
- "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "PageSource, PageIndex, Color, Opacity, Offset, TargetName, Property, Value, StartPoint, EndPoint",
+ "*:Freeze, mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

Vulnerator/Settings.XamlStyler at master · Vulnerator/Vulnerator

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransfom, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, Color, SolidColorBrush",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
- "Storyboard.*, From, To, Duration"
+ "Storyboard.*, From, To, Duration",
+ "HorizontalScrollBarVisibility, VerticalScrollBarVisibility",
+ "ElementName, Path, RelativeSource"
],

- "FirstLineAttributes": "",
+ "FirstLineAttributes": "x:Name",

XamlFlair/Settings.XamlStyler at master · XamlFlair/XamlFlair

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
+ "xmlns:mc",
+ "xmlns:d",
  "xmlns:*",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
- "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
+ "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "x:Key, Key, x:Name, Name",
+ "PlaceholderText, Text, x:Uid",
+ "Style",
+ "Foreground, FontSize, FontFamily, FontWeight, FontStyle, TextAlignment, TextWrapping, TextTrimming",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
- "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
- "*:*, *",
+ "HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Margin, Padding",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
- "Storyboard.*, From, To, Duration"
+ "Storyboard.*, From, To, Duration",
+ "*",
+ "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom, Panel.ZIndex",
+ "*:*"
],

Character-Map-UWP/CharacterMap/Settings.XamlStyler at master · character-map-uwp/Character-Map-UWP

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Style, Setter, SplineDoubleKeyFrame, KeySpline, EasingDoubleKeyFrame",

"AttributeOrderingRuleGroups": [
  "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
- "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
+ "x:Key, Key, x:Name, x:Load, x:DeferLoadStrategy, Name, x:Uid, Uid, Title",
+ "x:*",
+ "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom, ColumnSpacing, RowSpacing",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
- "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
+ "mc:Ignorable, d:*",
  "Storyboard.*, From, To, Duration"
],

Prism.Avalonia/Settings.XamlStyler at develop · AvaloniaCommunity/Prism.Avalonia

"AttributeOrderingRuleGroups": [
- "x:Class",
  "xmlns, xmlns:x",
  "xmlns:*",
+ "x:Class",
- "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
+ "x:Key, Key, x:Name, Name, x:Uid, Uid, Title, Text",
  "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
+ "Content",
  "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
  "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
+ "Source",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

- "FirstLineAttributes": "",
+ "FirstLineAttributes": "Text",

Uno.Gallery/Settings.XamlStyler at master · unoplatform/Uno.Gallery

- "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, StaticResource",
+ "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, StaticResource, utu:Shadow, d:Shadow",

Sharpnado.MaterialFrame/Maui.MaterialFrame/Settings.XamlStyler at master · roubachof/Sharpnado.MaterialFrame

"AttributeOrderingRuleGroups": [
  "x:Class",
- "xmlns, xmlns:x",
+ "xmlns, xmlns:x, xmlns:d, xmlns:mc, mc:Ignorable",
  "xmlns:*",
  "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
- "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
- "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
- "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
+ "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, AbsoluteLayout.LayoutFlags, AbsoluteLayout.LayoutBounds",
+ "Style, WidthRequest, HeightRequest, MinimumWidthRequest, MinimumHeightRequest",
+ "Margin, Padding, HorizontalOptions, VerticalOptions, HorizontalTextAlignment, VerticalTextAlignment",
  "*:*, *",
  "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
  "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
  "Storyboard.*, From, To, Duration"
],

CLI による自動フォーマット実行

https://github.com/Xavalon/XamlStyler/wiki/Script-Integration#install-as-a-local-tool

VS でターミナルを開き、以下のコマンドを実行します。

dotnet new tool-manifest

.config/dotnet-tools.json が作成されます。

{
  "version": 1,
  "isRoot": true,
  "tools": {}
}

以下のコマンドでインストールします。

dotnet tool install XamlStyler.Console
{
  "version": 1,
  "isRoot": true,
  "tools": {
    "xamlstyler.console": {
      "version": "3.2404.2",
      "commands": [
        "xstyler"
      ]
    }
  }
}

以下のようなコマンドで実行します。

dotnet xstyler --directory "SampleApp1" -recursive --config "../Settings.XamlStyler"

--config は指定した方が効率的です。
https://github.com/Xavalon/XamlStyler/blob/Release-3.2404.2/src/XamlStyler.Console/XamlStylerConsole.cs#L253-L279

また、ビルド時に自動的にフォーマットすることもできると思います。

SampleApp1.csproj (⚠️このコードは未検証です)

<Target Name="MyXAMLStyler_Restore" BeforeTargets="Restore;CollectPackageReferences">
   <Exec Command="dotnet tool restore"  StandardOutputImportance="Low" StandardErrorImportance="High"/>
</Target>

<Target Name="MyXAMLStyler_BeforeBuild" BeforeTargets="BeforeBuild">
  <!-- <Exec Command="dotnet tool restore" /> -->
  <Exec Command="dotnet xstyler --directory '$(ProjectDir)' -recursive --config '../Settings.XamlStyler'" />
</Target>

別のPCでは一度 dotnet tool restore を実行する必要がある点に注意してください。

感謝

2024-12-09 (月)