XAML 如何一致地对WPF标记属性进行排序

7kjnsjlb  于 2023-09-28  发布在  其他
关注(0)|答案(3)|浏览(130)

在使用WPF时,我在XAML和设计器之间切换。这会导致每个元素上的属性的顺序不同,并且使代码更难阅读,除非我手动重新排序所有内容。
有更简单的方法吗?

<TextBox Name="txtFirstName" Text="{Binding Path=FirstName, Mode=Default}" Grid.Column="1" Margin="6,2,31,11" Grid.Row="5" />

<TextBox Grid.RowSpan="2" Text="{Binding Path=LastName, Mode=Default}" Margin="6,0,31,4" Name="txtLastName" Grid.Column="1" Height="25" Grid.Row="3" VerticalAlignment="Bottom" />
2cmtqfgy

2cmtqfgy1#

我使用XAML Styler扩展名来格式化我的XAML文件。它会根据可配置的优先级自动对属性进行排序,并为您做一些其他不错的格式设置。

rm5edbpk

rm5edbpk2#

查看Visual Studio的Document Outline window(在视图-其他Windows中)。它将文档结构显示为一个树,并允许您快速定位到任何元素中,因此它可能对您有用。

nkhmeac6

nkhmeac63#

我强烈建议您使用以下XAML Styler配置:
1.下载适用于VS 2022 https://marketplace.visualstudio.com/items?itemName=TeamXavalon.XAMLStyler的Xaml Styler扩展
1.将下面的代码保存在一个名为Settings.xamlStyler的文件中,并将其放在解决方案(.sln)文件的同一文件夹中。

{
        "AttributesTolerance": 1,
        "KeepFirstAttributeOnSameLine": true,
        "MaxAttributeCharactersPerLine": 0,
        "MaxAttributesPerLine": 1,
        "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
        "SeparateByGroups": false,
        "AttributeIndentation": 0,
        "AttributeIndentationStyle": 0,
        "RemoveDesignTimeReferences":  false,
        "IgnoreDesignTimeReferencePrefix": false,
        "EnableAttributeReordering": true,
        "FirstLineAttributes": "x:Class, Grid.Row, Grid.Column, Grid.RowSpan, Grid.ColumnSpan, DockPanel.Dock, Orientation",
        "AttributeOrderingRuleGroups": [
            "d:DataContext",
            "xmlns, xmlns:x",
            "xmlns:*",
            "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
            "x:Class, Grid.Row, Grid.Column, Grid.RowSpan, Grid.ColumnSpan, DockPanel.Dock, Orientation",
            "Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
            "DataContext, Text, Content, Source, Command, CommandParameter, ItemSource, ItemsSource",
            "DisplayMemberPath, SelectedItem, SelectedValue, SelectedIndex",
            "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
            "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
            "UpImageSource, DownImageSource",
            "IsEnabled, Visibility",
            "*:*, *",
            "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
            "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
            "Storyboard.*, From, To, Duration"
        ],
        "OrderAttributesByName": true,
        "PutEndingBracketOnNewLine": false,
        "RemoveEndingTagOfEmptyElement": true,
        "SpaceBeforeClosingSlash": true,
        "RootElementLineBreakRule": 0,
        "ReorderVSM": 1,
        "ReorderGridChildren": false,
        "ReorderCanvasChildren": false,
        "ReorderSetters": 3,
        "FormatMarkupExtension": false,
        "NoNewLineElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter, Style",
        "NoNewLineMarkupExtensions": "x:Bind, Binding",
        "ThicknessSeparator": 2,
        "ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin",
        "PutAttributeOrderRuleGroupsOnSeparateLines": false,
        "FormatOnSave": true,
        "CommentPadding": 2,
        "SaveAndCloseOnFormat": true }

1.右键单击XAML文件或整个项目,然后单击“格式化XAML”以正确格式化文件。
此配置还将在保存时自动格式化任何xaml文件。
它还可以与.NET MAUI一起使用,可能还可以与UWP和WinUI一起使用。

相关问题