《Programming WPF》翻譯 第8章 5.建立動畫過程

來源:互聯網
上載者:User

所有在這章使用xaml舉例說明的技術,都可以在代碼中使用,正如你希望的。可是,代碼可以使用動畫在某種程度上不可能在xaml中實現的。

在代碼中建立動畫需要稍微多一點的努力——比使用標記。然而,代碼提供了更多的彈性。你可以在運行期計算屬性,而不是在xaml中寫入程式碼,從而支援你的動畫適應環境。例如,這可能是有用的——在當前表單的大小基於動畫的參數。

使用代碼一個額外的好處是我們不需要使用storyboard,替代的,我們可以建立一些被稱為“本地動畫”的對象。“本地動畫”直接應用到一個特定的屬性,這並不是storyboard的一部分。對於簡單的動畫,“本地動畫”的使用不比storyboard簡單。猜想你的標記中包含下面的橢圓:

<Ellipse x:Name=”theEllipse” Width=”50” Height=”100” Fill=”Red” />

你可以為此在代碼中建立和載入一個“本地動畫”,8-26所示:

樣本8-26

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.ClockController.Begin( );

我們不僅不需要把這些放入storyboard集合中我們還不需要SetterTimeline。使用storyboard,SetterTimeline需要指出哪個對象和屬性被設定了動畫。使用“本地動畫”,你可以直接將動畫添加到目標對象的PersistentAnimation集合,詳細指出該屬性作為集合中的索引。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.