with one click
format-style
工程代码格式化风格指引
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
工程代码格式化风格指引
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
| name | format-style |
| description | 工程代码格式化风格指引 |
若有未详尽覆盖的情况,可参考任意 src 下的代码,但不包括 src/lib 里的第三方或子模块代码
.axaml 和 .axaml.cs 文件要成对维护。net10.0,启用了 nullable,LangVersion 是 latest,并且默认启用编译绑定。.editorconfig 中的设置。using 指令不要用空行拆分导入组。var,包括内置类型和一眼可见的类型。nullable,并在所有地方保证空安全,不要留下 null 警告。对于临时为 null 的瞬态,可以用 null!。is 模式匹配而不是 == 或 !=:count is 0、count is not 0、value is null、value is not null、value is not A and not B。obj is { } value 绑定非空值。entry is { Width: > 0, Height: > 0 }。is not { ... } 做守卫。switch 表达式 > switch 语句 > if/else if 链,尤其适合枚举、类 union 的状态、命令和类型分发。[]、[.. items])、空条件运算、??、throw 表达式、范围/索引运算符、元组命名,以及清晰时的匿名成员推断。if-else 中若主体是一行以内,则可以省略大括号;若分支中有跳转语句(如return、break、continue 等)则优先写在 if 中,并且省略 else 来减少嵌套层级。(Type) value。using var 或 await using var,不要嵌套多层 using 块。using var 或 await using var 若有初始化列表则拆开,避免初始化时发生异常导致资源未正确释放。例如:using var a = new A { X = 1 }; 应该改为 using var a = new A(); a.X = 1;。void 返回值时,使用弃元(_ = ...;),表达式主体的方法除外。switch,使用 ArgumentOutOfRangeException(nameof(value)) 或等价的默认分支。Task 或 Task<T> 的方法,若只有返回时用到 Task,则不需要将方法声明为异步(async)方法,而是直接返回 Task,但方法名仍然需要 Async 后缀。CancellationToken token = default,并在其中所有 await 的 Task 使用 ConfigureAwait(false) 避免切换线程上下文。但在 UI 代码中,不要这样做以避免死锁。nameof(...) 而不是字符串字面量,包括字符串内插中。Lazy<T>、FrozenDictionary、生成正则,或者静态只读转换器实例,不要每次调用都重建数据。IDisposable,若需要则调用 GC.SuppressFinalize(this)。static/readonly/const/getonly/initonly 等以增强约束。stackalloc、Span<T>、Memory<T> 等来增强性能。<inheritdoc /> 来继承文档。<inheritdoc cref="..." /> 来复用文档。<see cref="..." />,并且不要使用全限定名,而是也用 using 引入。<paramref name="..." />、<typeparamref name="..." /> 来提供链接。<see langword="..." /> 来增强高亮。IReadOnlyCollection<T> 或更严格的 IReadOnlyList<T> 而不是 IEnumerable<T>,以表达集合内元素有限。IReadOnlyList<T>或ReadOnlySpan<T> 而不是 T[] 来表示数组,以避免潜在的数组协变。.Commands.cs 的 partial 文件。Try* 命名(例如 TryReset、TryDelete)。I 开头。_camelCase;私有静态字段和 static readonly 字段使用 _PascalCase。基础代码风格遵循 Settings.XamlStyler 中的设置。
尽量使用有类型信息的 XAML:
x:DataType="{x:Type vm:SomeViewModel}"TargetType="{x:Type Button}"x:Key="{x:Type controls:SomeControl}"BasedOn="{StaticResource {x:Type ListBox}}"对于 XAML 内的命名空间引用,使用 xmlns:prefix="using:..." 而不是 xmlns:prefix="clr-namespace:...;assembly=..."。对于根元素类所在的命名空间,使用 local 作为命名空间 xmlns:local="using:..."。
空元素使用自闭合标签。
稳定的样式/模板优先用 StaticResource,和主题相关的画刷/颜色优先用 DynamicResource。
优先使用编译/有类型绑定配合 x:DataType。
对控件主题/模板,使用 ControlTheme、ControlTemplate、诸如 PART_ContentPresenter 的命名部件,以及 TemplateBinding 处理模板属性。
优先使用标记扩展以减少代码行数,例如使用 {markup:SymbolIcon ...} 而不是 <fluent:SymbolIcon Symbol="..." />
文本布局优先使用 TextTrimming、MaxLines、TextWrapping 和资源文本主题,而不是自己写测量逻辑。
标准可绑定控件状态使用 Avalonia StyledProperty,非样式属性或用户控件中定义的需要绑定的属性使用 DirectProperty 和 SetAndRaise。
绑定风格:
$parent[...]、#Name等。IsVisible="{Binding !!Items.Count}"、{Binding !IsFollowed}。<ControlTheme x:Key="{x:Type controls:AvatarImage}" TargetType="{x:Type controls:AvatarImage}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type controls:AvatarImage}">
<Image Source="{TemplateBinding Source}" />
</ControlTemplate>
</Setter>
</ControlTheme>