当前位置: 首页 > news >正文

笔记:如何使用XAML Styler以及在不同的开发环境中使用一致

一、目的:分享如何使用XAML Styler以及在不同的开发环境中使用一致

        XAML Styler 是一个 Visual Studio 扩展,用于自动格式化和整理 XAML 文件。它可以帮助开发者保持一致的代码风格,提高代码的可读性和可维护性。以下是如何在 Visual Studio 中安装和使用 XAML Styler 的详细步骤。 


二、安装 XAML Styler


1.    打开 Visual Studio。
2.    导航到 扩展 > 管理扩展。
3.    在扩展管理器中,搜索 XAML Styler。
4.    点击 下载 并安装 XAML Styler 扩展。
5.    重启 Visual Studio 以完成安装。


三、配置 XAML Styler


1.    打开 XAML Styler 设置:
•    在 Visual Studio 中,导航到 工具 > 选项。
•    在选项窗口中,找到 XAML Styler。
2.    配置 XAML Styler:
•    在 XAML Styler 设置中,您可以根据需要配置各种选项,如属性排序规则、缩进设置、换行规则等。


四、创建 settings.xamlstyler 配置文件(可选)


如果您希望在项目中使用自定义的 XAML Styler 配置并且在不同的开发环境中使用一致,可以创建一个 settings.xamlstyler 文件


1.    创建 settings.xamlstyler 文件:


•    在项目的根目录中创建一个名为 settings.xamlstyler 的文件。

 


2.    添加配置:


•    在 settings.xamlstyler 文件中添加所需的配置。

例如:

{
    "AttributesTolerance": 1,
    "KeepFirstAttributeOnSameLine": true,
    "MaxAttributeCharactersPerLine": 0,
    "MaxAttributesPerLine": 1,
    "NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
    "SeparateByGroups": false,
    "AttributeIndentation": 0,
    "AttributeIndentationStyle": 1,
    "RemoveDesignTimeReferences": 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": 1,
    "ReorderGridChildren": false,
    "ReorderCanvasChildren": false,
    "ReorderSetters": 0,
    "FormatMarkupExtension": true,
    "NoNewLineMarkupExtensions": "x:Bind, Binding",
    "ThicknessSeparator": 2,
    "ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin",
    "FormatOnSave": true,
    "CommentPadding": 2,
    "IndentSize": 4
}

3.    配置 XAML Styler 使用 settings.xamlstyler:


•    在 XAML Styler 设置中,找到 Search to drivers root,将该值设置成true(此时就会到根目录去找settings.xamlstyler配置)。
•    或者选择 External configuration file,并指定 settings.xamlstyler 文件的路径(本人没有测试是否可用)。


五、使用 XAML Styler 格式化 XAML 文件


1.    手动格式化:


•    打开一个 XAML 文件。
•    右键点击文件内容,选择 Format Xaml或使用快捷键应用格式化。


2.    自动格式化:


•    如果在 settings.xamlstyler 文件中启用了 FormatOnSave 选项,保存 XAML 文件时将自动应用格式化。


六、示例


以下是一个示例 XAML 文件,展示了如何使用 XAML Styler 格式化:
原始 XAML 文件

<Window x:Class="H.Test.Halcon.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ha="http://schemas.mvtec.com/halcondotnet" xmlns:local="clr-namespace:H.Test.Halcon" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:s="clr-namespace:SkiaSharp.Drawing2D.Shape.Shape;assembly=SkiaSharp.Drawing2D.Shape" Title="MainWindow" Width="800" Height="450" mc:Ignorable="d">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top" LastChildFill="False">
            <Button Command="{Binding LoadCommand}" Content="Load" />
            <Button Command="{Binding CreateCommand}" Content="Create Model" />
            <Button Command="{Binding ApplyCommand}" Content="Apply" />
            <Button Command="{Binding ApplyModelCommand}" Content="Apply Model" />
            <Button Command="{Binding FindShapeCommand}" Content="Find Shape" />
        </DockPanel>
        <TabControl>
            <TabItem Header="Image">
                <Image Source="{Binding ImageSource}" />
            </TabItem>
            <TabItem Header="ViewStateShapeView">
                <Grid>
                    <ViewStateShapeView>
                        <ViewStateShapeView.Shape>
                            <s:ImageBluePrintShape ImageSource="{GetAssetsSKBitmapMarkupExtension 1.jpg}" />
                        </ViewStateShapeView.Shape>
                        <ViewStateShapeView.InputShapes>
                        </ViewStateShapeView.InputShapes>
                    </ViewStateShapeView>
                </Grid>
            </TabItem>
            <TabItem Header="ViewStateShapeView">
                <Grid>
                    <ViewStateShapeView Shape="{Binding Shape}" Shapes="{Binding Shapes}" />
                </Grid>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>

 格式化后的 XAML 文件

<Window x:Class="H.Test.Halcon.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:ha="http://schemas.mvtec.com/halcondotnet"
        xmlns:local="clr-namespace:H.Test.Halcon"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:s="clr-namespace:SkiaSharp.Drawing2D.Shape.Shape;assembly=SkiaSharp.Drawing2D.Shape"
        Title="MainWindow"
        Width="800"
        Height="450"
        mc:Ignorable="d">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <DockPanel>
        <DockPanel DockPanel.Dock="Top"
                   LastChildFill="False">
            <Button Command="{Binding LoadCommand}"
                    Content="Load" />
            <Button Command="{Binding CreateCommand}"
                    Content="Create Model" />
            <Button Command="{Binding ApplyCommand}"
                    Content="Apply" />
            <Button Command="{Binding ApplyModelCommand}"
                    Content="Apply Model" />
            <Button Command="{Binding FindShapeCommand}"
                    Content="Find Shape" />
        </DockPanel>
        <TabControl>
            <TabItem Header="Image">
                <Image Source="{Binding ImageSource}" />
            </TabItem>
            <TabItem Header="ViewStateShapeView">
                <Grid>
                    <ViewStateShapeView>
                        <ViewStateShapeView.Shape>
                            <s:ImageBluePrintShape ImageSource="{GetAssetsSKBitmapMarkupExtension 1.jpg}" />
                        </ViewStateShapeView.Shape>
                        <ViewStateShapeView.InputShapes>
                        </ViewStateShapeView.InputShapes>
                    </ViewStateShapeView>
                </Grid>
            </TabItem>
            <TabItem Header="ViewStateShapeView">
                <Grid>
                    <ViewStateShapeView Shape="{Binding Shape}"
                                        Shapes="{Binding Shapes}" />
                </Grid>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>

七、总结


        通过上述步骤,您可以在 Visual Studio 中安装和使用 XAML Styler,自动格式化和整理 XAML 文件。这样可以确保所有 XAML 文件遵循一致的格式化规则,提高代码的可读性和可维护性。

        如果想在多平台共用配置可以参考四部分的配置。

需要了解的知识点 

XAML概述 - WPF .NET | Microsoft Learn

XAML 语法详述 - WPF .NET Framework | Microsoft Learn 

XAML - WPF .NET Framework | Microsoft Learn 

标记扩展和 XAML - WPF .NET Framework | Microsoft Learn 

XAML 及自定义类 - WPF .NET Framework | Microsoft Learn 

XAML 命名空间 (x:) 语言功能 - XAML | Microsoft Learn 

XAML 扩展 - WPF .NET Framework | Microsoft Learn 

基元素概述 - WPF .NET Framework | Microsoft Learn 

System.Windows.Controls 命名空间 | Microsoft Learn

控件库 - WPF .NET Framework | Microsoft Learn

WPF 介绍 | Microsoft Learn

Windows Presentation Foundation 简介 - WPF .NET | Microsoft Learn

使用 Visual Studio 创建新应用教程 - WPF .NET | Microsoft Learn

了解更多

适用于 .NET 8 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

适用于 .NET 7 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

System.Windows.Controls 命名空间 | Microsoft Learn

Reference Source

Sysinternals - Sysinternals | Microsoft Learn

Windows app development documentation - Windows apps | Microsoft Learn

欢迎使用 Expression Blend | Microsoft Learn

https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/?view=netdesktop-7.0&WT.mc_id=MVP_380318

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

相关文章:

  • 第7章 wireshark(网络安全防御实战--蓝军武器库)
  • 阿里云 DataWorks面试题集锦及参考答案
  • Visual Studio 2022新建c语言项目的详细步骤
  • 文献学习: 单细胞+肿瘤转移研究的发文思路解析:如何构建核心基因特征,揭示关键调控网络?
  • VB6 调用 JS 函数时数据传输json格式或a=1b=s2字符串
  • 十倍烈火刀刀爆?伪随机分布(PRD)算法详解与C++实现
  • 洛谷P1091
  • 记录排查服务器CPU负载过高
  • 【自学笔记】OpenStack基础知识点总览-持续更新
  • nvidia驱动升级-ubuntu 1804
  • 系统架构设计师—数据库基础篇—关系代数运算
  • FreeRTOS 任务管理与运行时间统计:API 解析与配置实践
  • SQLAlchemy系列教程:集成Pydantic增强数据处理能力
  • 7.自然语言处理(NLP)理论基础——大模型微调的基石
  • 【蓝桥杯集训·每日一题2025】 AcWing 5539. 牛奶交换 python
  • C语言番外篇(4)------------------>VS环境下源码的隐藏
  • Dify部署踩坑指南(Windows+Mac)
  • Spring Boot 项目中 Redis 常见问题及解决方案
  • 常见的限流算法有哪些?
  • 社区智慧养老标准规范全解析
  • 企业网站策划书ppt/如何申请一个网站域名
  • 原创 网站 源码/优化网站怎么做
  • 网站建站哪个品牌好/百度服务
  • 旅游商业网站策划书/360竞价推广客服电话
  • 斗米兼职做任务发兼职网站靠谱吗/百度一下你就知道了
  • 武汉优联网站建设/巩义网络推广公司