不需 Storyboard 與 SetterTimeline,直接將動畫加入目標元素的 PersistentAnimations 集合,以目標屬性為索引:
<Ellipse x:Name="theEllipse" Width="50" Height="100" Fill="Red" />
DoubleAnimation widthAnimation = new DoubleAnimation();
widthAnimation.By = 100;
widthAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
theEllipse.PersistentAnimations[Ellipse.WidthProperty] = widthAnimation;
Clock clock = theEllipse.PersistentAnimations.GetClock(Ellipse.WidthProperty);
clock.Controller.Begin();
PersistentAnimations 直接對應目標屬性,無需指定 TargetName/Path。Clock 後透過 Controller 控制播放(Begin、Pause、Stop、Seek...)。| 特性 | Storyboard (XAML/程式碼) | 本地動畫 (程式碼) |
|---|---|---|
| 目標指定 | SetterTimeline + TargetName/Path | PersistentAnimations[DP] |
| 多屬性協調 | ParallelTimeline/SequenceTimeline | 需自行協調多個 Clock |
| 觸發器整合 | EventTrigger 自動啟動 | 僅程式碼控制 |
| 動態參數 | 困難 | 容易 |
選擇建議:複雜協調、樣式/模板整合、觸發器驅動 → Storyboard;簡單、動態、一次性 → 本地動畫。