Skip to content

Switch 開關 ​

開關。膠囊軌道 + 圓形滑塊,點選切換;支援停用與載入中。

契約源:lh-ui-core/specs/switch.json · 黃金用例:lh-ui-core/cases/switch.json(6 條,已翻譯為 xUnit 測試)

基礎用法 ​

9:41Switch 基礎

HTML 模擬預覽(Windows / WinUI 3 外觀),非真機渲染

csharp
var sw = new LhSwitch { Checked = true };
sw.Change += isChecked => { };

尺寸 ​

三檔尺寸映射到 switch.width / switch.height 令牌;滑塊直徑 = 高度 - 2 × switch.gap。

9:41Switch 尺寸

HTML 模擬預覽(Windows / WinUI 3 外觀),非真機渲染

狀態 ​

Disabled 與 Loading 都不回應點選;Loading 時滑塊位置改畫主色載入環。

9:41Switch 狀態

HTML 模擬預覽(Windows / WinUI 3 外觀),非真機渲染

API ​

屬性 ​

屬性說明型別預設值
Change開關態變化回呼;disabled 或 loading 時不觸發event Action<bool>—
Checked是否開啟boolfalse
Disabled是否停用(不回應點選且呈現停用樣式)boolfalse
Loading是否載入中(滑塊位置顯示載入環且不回應點選)boolfalse
Size開關尺寸,映射到 switch.width / switch.height 令牌LhSwitchSizeMedium

LhSwitchSize ​

值widthheight
Smallswitch.width.small(40epx)switch.height.small(22epx)
Mediumswitch.width.medium(48epx)switch.height.medium(26epx)
Largeswitch.width.large(56epx)switch.height.large(30epx)

令牌對照 ​

令牌用途
switch.width.{small,medium,large}三檔軌道寬度
switch.height.{small,medium,large}三檔軌道高度
switch.gap滑塊與軌道留白(滑塊直徑 = 高度 - 2 × gap)
color.primary.default開啟軌道色
color.border.base關閉軌道色
color.onPrimary滑塊色
color.disabled.bg / color.text.disabled停用軌道/滑塊色

黃金用例對照 ​

cases/switch.json 的 6 條斷言與 xUnit 測試的對應關係(tests/LhSwitchTests.cs):

case id斷言本地測試
tap-toggles-and-emits-onchange點選切換並觸發 onChangeTap_Toggles_And_Emits_OnChange
tap-checked-turns-off開啟態點選關閉Tap_Checked_Turns_Off
disabled-blocks-change停用態不可互動Disabled_Blocks_Change
loading-blocks-change載入態不可互動Loading_Blocks_Change
checked-uses-primary-token開啟態消費主色令牌Checked_Uses_Primary_Token
unchecked-uses-border-token關閉態消費描邊令牌Unchecked_Uses_Border_Token

另有暗色專項:Dark_Colors_Come_From_Dark_Tokens / Dimensions_Are_Identical_In_Dark_Mode。 執行:cd lh-ui-windows && dotnet test。