- WPF提供了一種基於幀的動畫實現方式,由CompositionTarget類來完成。
- WPF會在每次界面刷新時調用該回調函數。
- CompositionTarget的刷新率與窗體保持一致,所以很難人工控制動畫的快慢。
一、前臺html
<Grid> <Grid.RowDefinitions> <RowDefinition Height=" *" /> <RowDefinition Height=" *" /> </Grid.RowDefinitions> <Rectangle Name="rectan" Width=" 30" Height=" 100" Fill="Blue" /> <Button Grid.Row=" 1" Width=" 100" Height=" 30" Click="Button_Click" /> </Grid>
二、後臺cs編程
public partial class MainWindow : Window { private double maxwi = 300; private double strart = 0; public MainWindow() { InitializeComponent(); strart = rectan.Width; } private void Button_Click(object sender, RoutedEventArgs e) { CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); } private void CompositionTarget_Rendering(object sender, EventArgs e) { rectan.Width += 1; if (rectan.Width > maxwi) { rectan.Width = strart; CompositionTarget.Rendering -= new EventHandler(CompositionTarget_Rendering); } }