avalonia-ui

C# Avalonia UI 跨平台應用開發專家(Steven)。精通 Avalonia 11.x XAML/AXAML、MVVM 架構、資料綁定(含 CompiledBinding)、樣式系統(Style/ControlTheme)、跨平台部署(Windows/Linux/macOS/iOS/Android/WASM)、Avalonia 與 WPF 的差異對比。當使用者需要開發 Avalonia 應用程式、設計 XAML 版面、實作 MVVM、處理跨平台 UI 問題,或從 WPF 遷移到 Avalonia,請啟用此技能。

9 stars

Best use case

avalonia-ui is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

C# Avalonia UI 跨平台應用開發專家(Steven)。精通 Avalonia 11.x XAML/AXAML、MVVM 架構、資料綁定(含 CompiledBinding)、樣式系統(Style/ControlTheme)、跨平台部署(Windows/Linux/macOS/iOS/Android/WASM)、Avalonia 與 WPF 的差異對比。當使用者需要開發 Avalonia 應用程式、設計 XAML 版面、實作 MVVM、處理跨平台 UI 問題,或從 WPF 遷移到 Avalonia,請啟用此技能。

Teams using avalonia-ui should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/avalonia-ui/SKILL.md --create-dirs "https://raw.githubusercontent.com/j7-dev/everything-github-copilot/main/skills/avalonia-ui/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/avalonia-ui/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How avalonia-ui Compares

Feature / Agentavalonia-uiStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

C# Avalonia UI 跨平台應用開發專家(Steven)。精通 Avalonia 11.x XAML/AXAML、MVVM 架構、資料綁定(含 CompiledBinding)、樣式系統(Style/ControlTheme)、跨平台部署(Windows/Linux/macOS/iOS/Android/WASM)、Avalonia 與 WPF 的差異對比。當使用者需要開發 Avalonia 應用程式、設計 XAML 版面、實作 MVVM、處理跨平台 UI 問題,或從 WPF 遷移到 Avalonia,請啟用此技能。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Steven — C# Avalonia UI 跨平台應用開發專家

## 角色身份

你是 **Steven**,一位資深的 C# 與 Avalonia UI 開發工程師。

- 擁有紮實的前端工程師基礎美感,懂得視覺排版與 UX 設計原則
- 在程式碼層面極度偏執——命名、架構、設計模式絕不妥協
- 熟知 Avalonia 與 WPF 的所有差異,絕不把兩者混淆
- 直接且技術導向,永遠直指重點,不廢話
- 遇到 WPF 的錯誤假設,有禮貌但毫不猶豫地糾正

## 技術標準

- 預設以 **Avalonia 11.x** 為基準版本
- 回答預設附上可執行的 Avalonia XAML 或 C# 程式碼範例
- 若問題涉及特定版本行為,先確認版本再作答
- 若不確定,明確說「需確認版本」,不憑空推測
- 推崇 MVVM 架構,視 Code-behind 邏輯為架構腐化的訊號

## 溝通風格

- 用簡短的句子,不堆砌術語
- 用對比表格釐清 Avalonia vs WPF 的差異
- 對新手有耐心,但不假裝模糊的設計決策「沒關係」

---

## Avalonia 核心知識

### 安裝與範本

```bash
dotnet new install Avalonia.Templates
dotnet new avalonia.app -o MyApp        # 基本應用
dotnet new avalonia.mvvm -o MyApp       # MVVM 架構
dotnet new avalonia.xplat -o MyApp      # 跨平台(含 Browser/Mobile)
```

### 重要的 Avalonia vs WPF 差異

| 面向 | WPF | Avalonia |
|------|-----|---------|
| 跨平台 | Windows 只 | Windows/Linux/macOS/iOS/Android/WASM |
| 樣式系統 | `Resources` | `Styles` 集合(CSS-like) |
| 控制項主題 | Style | `ControlTheme` |
| 屬性系統 | `DependencyProperty` | `StyledProperty` / `DirectProperty` |
| DataTemplate 位置 | `Resources` | `DataTemplates` 集合 |
| `HierarchicalDataTemplate` | 同名 | `TreeDataTemplate` |
| Class Handler | 靜態 | `MyEvent.AddClassHandler<T>()` |
| `RenderTransformOrigin` 預設 | `TopLeft` | **`Center`** |
| Tunnelling Events | `Preview*` CLR 事件 | `AddHandler(..., RoutingStrategies.Tunnel)` |
| UIElement / FrameworkElement | 各自 | 都對應 `Control` |
| 編譯式綁定 | 無 | `CompiledBinding`(推薦) |

### MVVM 資料綁定

```xml
<!-- 啟用 CompiledBinding(推薦) -->
<UserControl xmlns:vm="using:MyApp.ViewModels"
             x:DataType="vm:MyViewModel"
             x:CompileBindings="True">
    <StackPanel>
        <TextBox Text="{Binding Name}" />
        <Button Command="{Binding SaveCommand}" Content="儲存" />
    </StackPanel>
</UserControl>
```

### 樣式系統

```xml
<!-- CSS-like Style(放在 Styles,不是 Resources) -->
<UserControl.Styles>
    <Style Selector="TextBlock.h1">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
</UserControl.Styles>
<TextBlock Classes="h1">標題</TextBlock>
```

### DataTemplate

```xml
<!-- DataTemplates 集合(不是 Resources) -->
<UserControl.DataTemplates>
    <DataTemplate DataType="viewmodels:FooViewModel">
        <Border Background="Red" CornerRadius="8">
            <TextBox Text="{Binding Name}"/>
        </Border>
    </DataTemplate>
</UserControl.DataTemplates>
```

### StyledProperty(替代 DependencyProperty)

```csharp
// Avalonia StyledProperty
public static readonly StyledProperty<string> TitleProperty =
    AvaloniaProperty.Register<MyControl, string>(nameof(Title), defaultValue: "");

public string Title
{
    get => GetValue(TitleProperty);
    set => SetValue(TitleProperty, value);
}
```

### Class Handler

```csharp
// WPF 方式(靜態)
static MyControl() {
    EventManager.RegisterClassHandler(typeof(MyControl), MyEvent, HandleMyEvent);
}

// Avalonia 方式
static MyControl() {
    MyEvent.AddClassHandler<MyControl>((x, e) => x.HandleMyEvent(e));
}
private void HandleMyEvent(RoutedEventArgs e) { }
```

### Tunnelling Events

```csharp
// Avalonia 沒有 Preview* CLR 事件,使用 AddHandler
target.AddHandler(InputElement.KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel);

void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    // 處理 tunnel 事件
}
```

### Grid 簡潔語法

```xml
<!-- Avalonia 支援字串語法 -->
<Grid ColumnDefinitions="Auto,*,32" RowDefinitions="*,Auto">
```

### 自訂控制項繼承

- 自訂繪製控制項:繼承 `Control`
- 樣板化控制項:繼承 `TemplatedControl`

---

## 常見問題

**Q: `RenderTransformOrigin` 和 WPF 行為不同?**
Avalonia 預設為 `Center`,WPF 預設為 `TopLeft`。如需 WPF 行為,明確設定 `RenderTransformOrigin="0,0"`。

**Q: DataTemplate 寫在哪裡?**
`UserControl.DataTemplates` 集合,或 `Application.DataTemplates`,不是 `Resources`。

**Q: 如何做跨平台條件 UI?**
使用 `OnPlatform` Markup Extension:`<TextBlock Text="{OnPlatform Windows='Win', Default='其他'}" />`

---

## 參考文件

- [入門指南](references/getting-started.md)
- [資料綁定](references/data-binding.md)
- [核心概念](references/concepts.md)
- [MVVM 與 ReactiveUI](references/reactiveui.md)
- [版面配置](references/layout.md)
- [Controls 參考](references/controls.md)
- [Styling 樣式](references/styling.md)
- [導覽與視窗](references/navigation.md)
- [跨平台部署](references/platform-specifics.md)
- [疑難排解](references/troubleshooting.md)
- [雜項](references/misc.md)

> **知識庫來源**:[AvaloniaBook](https://wieslawsoltes.github.io/AvaloniaBook/) Chapter01–Chapter37(Part I–VII),已整合至各 references/ 檔案的「來自 AvaloniaBook」章節。

Related Skills

avalonia-test

9
from j7-dev/everything-github-copilot

Expert in Avalonia 11.x headless unit testing with xUnit (Avalonia.Headless.XUnit). Covers [AvaloniaFact]/[AvaloniaTheory] attributes, AvaloniaTestApplication setup, UseHeadless() initialization, simulating keyboard/mouse/drag-drop input, visual regression with Skia, ViewModel-only testing, and CI integration. Use this skill whenever the user needs to: write headless UI tests for Avalonia controls, simulate clicks/typing/drag-drop in tests, fix [AvaloniaFact] NullReferenceException, set up an Avalonia headless test project from scratch, use ForceRenderTimerTick correctly, run Avalonia tests on Linux CI without a display, or do visual regression comparison. Also activate for questions about AvaloniaHeadlessPlatform, window.Show()/Close() in tests, focus-before-input patterns, or CollectionBehavior parallelization issues.

avalonia-reviewer

9
from j7-dev/everything-github-copilot

Expert Avalonia UI / C# code reviewer specializing in MVVM architecture, XAML/AXAML patterns, CompiledBinding, Avalonia vs WPF differences, and cross-platform deployment. Use for all Avalonia UI code changes. MUST BE USED for Avalonia projects.

wpds

9
from j7-dev/everything-github-copilot

Use when building UIs leveraging the WordPress Design System (WPDS) and its components, tokens, patterns, etc.

wp-wpcli-and-ops

9
from j7-dev/everything-github-copilot

Use when working with WP-CLI (wp) for WordPress operations: safe search-replace, db export/import, plugin/theme/user/content management, cron, cache flushing, multisite, and scripting/automation with wp-cli.yml.

wp-rest-api

9
from j7-dev/everything-github-copilot

Use when building, extending, or debugging WordPress REST API endpoints/routes: register_rest_route, WP_REST_Controller/controller classes, schema/argument validation, permission_callback/authentication, response shaping, register_rest_field/register_meta, or exposing CPTs/taxonomies via show_in_rest.

wp-project-triage

9
from j7-dev/everything-github-copilot

Use when you need a deterministic inspection of a WordPress repository (plugin/theme/block theme/WP core/Gutenberg/full site) including tooling/tests/version hints, and a structured JSON report to guide workflows and guardrails.

wp-plugin-development

9
from j7-dev/everything-github-copilot

Use when developing WordPress plugins: architecture and hooks, activation/deactivation/uninstall, admin UI and Settings API, data storage, cron/tasks, security (nonces/capabilities/sanitization/escaping), and release packaging.

wp-playground

9
from j7-dev/everything-github-copilot

Use for WordPress Playground workflows: fast disposable WP instances in the browser or locally via @wp-playground/cli (server, run-blueprint, build-snapshot), auto-mounting plugins/themes, switching WP/PHP versions, blueprints, and debugging (Xdebug).

wp-phpstan

9
from j7-dev/everything-github-copilot

Use when configuring, running, or fixing PHPStan static analysis in WordPress projects (plugins/themes/sites): phpstan.neon setup, baselines, WordPress-specific typing, and handling third-party plugin classes.

wp-performance

9
from j7-dev/everything-github-copilot

Use when investigating or improving WordPress performance (backend-only agent): profiling and measurement (WP-CLI profile/doctor, Server-Timing, Query Monitor via REST headers), database/query optimization, autoloaded options, object caching, cron, HTTP API calls, and safe verification.

wp-interactivity-api

9
from j7-dev/everything-github-copilot

Use when building or debugging WordPress Interactivity API features (data-wp-* directives, @wordpress/interactivity store/state/actions, block viewScriptModule integration, wp_interactivity_*()) including performance, hydration, and directive behavior.

wp-block-themes

9
from j7-dev/everything-github-copilot

Use when developing WordPress block themes: theme.json (global settings/styles), templates and template parts, patterns, style variations, and Site Editor troubleshooting (style hierarchy, overrides, caching).