AgentSkillsCN

winui-xaml

WinUI 3与XAML的最佳实践,涵盖布局、绑定、异步操作、集合、渲染、内存管理、无障碍性与本地化。在以下情况下使用此功能:(1) 使用WinUI 3或Uno Platform XAML进行开发;(2) 优化UI性能;(3) 使用x:Bind实现数据绑定;(4) 管理集合与虚拟化;(5) 确保无障碍性合规;(6) 在UI线程上处理异步操作;(7) 防止内存泄漏;(8) 实现本地化。

SKILL.md
--- frontmatter
name: winui-xaml
description: "WinUI 3 and XAML best practices for layout, binding, async operations, collections, rendering, memory management, accessibility, and localization. Use when: (1) Working with WinUI 3 or Uno Platform XAML, (2) Optimizing UI performance, (3) Implementing data binding with x:Bind, (4) Managing collections and virtualization, (5) Ensuring accessibility compliance, (6) Handling async operations on UI thread, (7) Preventing memory leaks, (8) Implementing localization"

WinUI 3 XAML Best Practices

Twelve categories of XAML best practices validated against WinUI 3 and Uno Platform, covering performance-critical patterns through polish optimizations.

Quick Reference - Top 10 Rules

  1. Never block the UI thread - Use async/await for all I/O and long operations
  2. Use x:Bind over Binding - Compile-time validation, 3x faster
  3. Use x:Load for conditional UI - Prevents element creation until needed
  4. Enable virtualization - ListView/ItemsRepeater for any list > 20 items
  5. Unsubscribe event handlers - Clean up in Unloaded to prevent leaks
  6. Keep visual tree shallow - Minimize nesting depth
  7. Use OneTime mode for static data - Avoid unnecessary change tracking
  8. Set AutomationProperties - Required for accessibility compliance
  9. Use x:Uid for all user-visible text - Enables localization
  10. Prefer Grid over nested StackPanels - Single layout pass vs multiple

Categories by Impact

#CategoryImpactKey Topics
1LayoutHIGHPanel selection, visual tree depth, AutoLayout, responsive design
2BindingHIGHx:Bind vs Binding, modes, function binding
3Async & ThreadingCRITICALUI thread, DispatcherQueue, parallelization
4Collections & VirtualizationCRITICALListView, ItemsRepeater, incremental loading
5Rendering & CompositionMEDIUM-HIGHComposition APIs, animations, caching
6Memory ManagementHIGHEvent handlers, leaks, resource cleanup
7AccessibilityHIGHAutomationProperties, keyboard navigation
8LocalizationMEDIUMx:Uid, .resw files, runtime switching
9Styles & ThemingMEDIUMResourceDictionary, ThemeDictionaries, lightweight styling
10XAML LoadingCRITICALx:Load, deferred loading, startup optimization
11NavigationMEDIUM-HIGHFrame caching, page lifecycle
12Input & InteractionMEDIUMTouch targets, gesture handling

Impact Levels

  • CRITICAL: Can cause app crashes, multi-second freezes, or memory leaks
  • HIGH: Noticeable performance degradation (100ms+)
  • MEDIUM-HIGH: Cumulative impact across many elements
  • MEDIUM: Optimization for polish and responsiveness

Pattern Structure

Each reference file follows this format:

markdown
### Skill Name
**Rule**: Concise imperative statement
**Why**: Technical rationale with measurable impact
**Example (XAML/C#)**: Correct implementation
**Common Mistakes**: Anti-patterns to avoid
**Uno Platform Notes**: Platform-specific gaps/workarounds (if applicable)
**Reference**: Microsoft Learn link

WinUI 3 vs UWP Key Differences

FeatureUWPWinUI 3
NamespaceWindows.UI.XamlMicrosoft.UI.Xaml
DispatcherCoreDispatcherDispatcherQueue
Deferred Loadingx:DeferLoadStrategy (deprecated)x:Load
WindowCoreWindowMicrosoft.UI.Xaml.Window
App ModelUWP sandboxDesktop (full trust)

Uno Platform Compatibility

All patterns validated against Uno Platform with notes for:

  • Platform-specific behavior differences
  • Unsupported APIs and alternatives
  • Skia renderer considerations

Using References

Consult the individual reference files listed in the table above, matched to the task at hand. Each file is self-contained with rules, code examples, common mistakes, and Uno Platform compatibility notes.