Skip to content

Countdown 倒計時 ​

牆鐘差值驅動的倒計時:背景切換不漂移,受控直顯與非受控自走兩種模式,格式化令牌 DD/HH/mm/ss/S。

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

端側落點說明

剩餘時間由「目標牆鐘 − now」換算(LhCountdownTokens.Engine),鉗制 ≥ 0,不用 DispatcherTimer 累減——背景切換 / 定時器節流不漂移;DispatcherTimer 只負責每步觸發重算 (millisecond=true 時 16ms / 否則 250ms)。實例方法 Start / Pause / Reset 由控制項直接暴露, 無需額外控制器句柄。

基礎用法 ​

非受控模式:傳入 Time 總時長,AutoStart(預設 true)時掛載自走;LhOnChange 每步回呼剩餘毫秒,歸零觸發 LhOnFinish 一次。

9:41Countdown 基礎用法
01:00:00

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

csharp
var countdown = new LhCountdown { Time = 60 * 60 * 1000 };
countdown.LhOnChange += remaining => { /* 剩餘毫秒 */ };
countdown.LhOnFinish += () => { /* 歸零觸發一次 */ };

格式化令牌 ​

Format 支援 DD(天)/ HH(時)/ mm(分)/ ss(秒)/ S(毫秒三位),其餘字元按字面量渲染;無 DD 時 HH 承載總小時數(可超 24);負值鉗零。

9:41Countdown 格式化令牌
01:01:05
01天00:00:01.500
50:30:10

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

csharp
new LhCountdown { Value = 86401500, Format = "DD天HH:mm:ss.S" }; // 01天00:00:01.500
new LhCountdown { Value = 181810000, Format = "HH:mm:ss" };      // 50:30:10(無 DD,HH 承載總小時)
new LhCountdown { Value = -1 };                                   // 00:00:00(負值鉗零)

受控與非受控 ​

Value 傳入即受控:元件不自驅動,顯示外部值(受控回寫同走 LhOnChange); Time 傳入即非受控:AutoStart 時掛載自走。Disabled 灰化並暫停計時(還原後不自動續走,由 Start 接續)。

9:41Countdown 受控與非受控
00:10:0000:00:00

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

csharp
new LhCountdown { Value = 10 * 1000, Disabled = true }; // 受控直顯 + 停用灰化
countdown.Pause();  // 記住剩餘
countdown.Start();  // 暫停點續走(disabled 時不生效)
countdown.Reset();  // 回 Time 總時長並可按 AutoStart 重啟

API ​

屬性 ​

屬性說明類型預設值
Value剩餘毫秒(受控直顯:傳入後元件不自驅動)long?null
Time非受控總時長 ms(掛載後 autoStart 自走)long0
Format格式化串;令牌 DD/HH/mm/ss/Sstring"HH:mm:ss"
AutoStart非受控模式掛載後是否自動開始booltrue
Millisecond逐幀重新整理(步進 16ms / 否則 250ms)boolfalse
Disabled停用:灰化並暫停計時boolfalse
AriaLabel無障礙名稱(未傳取格式化文案)string?null

事件 ​

事件載荷說明
LhOnChangelong remaining剩餘毫秒變化(非受控每步回呼;受控回寫同走此事件)
LhOnFinish—倒計時歸零時觸發一次

方法 ​

方法說明
Start()暫停點續走(disabled 時不生效)
Pause()記住剩餘
Reset()回 Time 總時長並可按 AutoStart 重啟

令牌對照 ​

令牌用途
countdown.digitMinWidth數值位最小寬度(等寬防抖)
typography.fontSize.large數字字級
color.text.primary數值位顏色
color.text.secondary分隔字面量顏色(弱化)
color.text.disabled停用灰化色

黃金用例對照 ​

cases/countdown.json 的 10 條斷言與 xUnit 測試的對應關係(tests/LhCountdownTests.cs):

case id斷言本地測試
renders-formatted-remaining受控值按預設格式渲染 01:01:05Renders_Formatted_Remaining
negative-clamps-to-zero負值鉗制為全零Negative_Clamps_To_Zero
hours-over-24-without-dd無 DD 時 HH 承載總小時數 50:30:10Hours_Over_24_Without_Dd
dd-format-splits-daysDD 格式按天拆分且毫秒三位補零Dd_Format_Splits_Days
uncontrolled-autostart-ticks非受控 autoStart 掛載自走並回呼 onChangeUncontrolled_Autostart_Ticks
finish-emits-once-at-zero歸零觸發 onFinish 一次Finish_Emits_Once_At_Zero
controlled-does-not-self-drive受控模式不自驅動Controlled_Does_Not_Self_Drive
autostart-false-waits-for-startautoStart=false 等待 start() 起算Autostart_False_Waits_For_Start
disabled-pauses-and-greys停用灰化且不驅動(color.text.disabled 應用)Disabled_Pauses_And_Greys
aria-role-timer可達名稱兜底格式化文案Aria_Name_Falls_Back_To_Formatted_Text

另有補充斷言:Digit_Min_Width_Token_And_Separator_Weakened / Reset_Returns_To_Total_And_Can_Restart 以及暗色專項 Dark_Colors_Come_From_Dark_Tokens / Dimensions_Are_Identical_In_Dark_Mode。 執行:cd lh-ui-windows && dotnet test。