WPF備忘錄四(事件動畫)
<Grid>
<Rectangle Height="65" HorizontalAlignment="Left" Fill="Blue" Margin="133,67,0,0" Name="rect" Stroke="Black" VerticalAlignment="Top" Width="73">
<Rectangle.RenderTransform>
<RotateTransform Angle="0" CenterX="20" CenterY="20" x:Name="rt"></RotateTransform>
</Rectangle.RenderTransform>
</Rectangle>
<Button Content="開始" Height="23" HorizontalAlignment="Left" Margin="32,189,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" />
<Button Content="暫停" Height="23" HorizontalAlignment="Left" Margin="113,189,0,0" Name="btnPause" VerticalAlignment="Top" Width="75" />
<Button Content="繼續" Height="23" HorizontalAlignment="Left" Margin="194,189,0,0" Name="btnResume" VerticalAlignment="Top" Width="75" />
<Button Content="停止" Height="23" HorizontalAlignment="Right" Margin="0,189,153,0" Name="btnStop" VerticalAlignment="Top" Width="75" />
<Grid.Triggers>
<!--開始-->
<EventTrigger SourceName="btnStart" RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard x:Name="bs">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="rt" Storyboard.TargetProperty="Angle" From="0" To="360" Duration="00:00:10"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<!--暫停-->
<EventTrigger SourceName="btnPause" RoutedEvent="Button.Click">
<EventTrigger.Actions>
<PauseStoryboard BeginStoryboardName="bs"></PauseStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<!--繼續-->
<EventTrigger SourceName="btnResume" RoutedEvent="Button.Click">
<ResumeStoryboard BeginStoryboardName="bs"></ResumeStoryboard>
</EventTrigger>
<!--停止-->
<EventTrigger SourceName="btnStop" RoutedEvent="Button.Click">
<StopStoryboard BeginStoryboardName="bs"></StopStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
利用程式碼方式處理動畫事件:
<Grid>
<Rectangle Height="65" HorizontalAlignment="Left" Fill="Blue" Margin="133,67,0,0" Name="rect" Stroke="Black" VerticalAlignment="Top" Width="73">
<Rectangle.RenderTransform>
<RotateTransform Angle="0" CenterX="20" CenterY="20" x:Name="rt"></RotateTransform>
</Rectangle.RenderTransform>
</Rectangle>
<Button Content="開始" Height="23" HorizontalAlignment="Left" Margin="32,189,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" Click="btnStart_Click" />
<Button Content="暫停" Height="23" HorizontalAlignment="Left" Margin="113,189,0,0" Name="btnPause" VerticalAlignment="Top" Width="75" />
<Button Content="繼續" Height="23" HorizontalAlignment="Left" Margin="194,189,0,0" Name="btnResume" VerticalAlignment="Top" Width="75" />
<Button Content="停止" Height="23" HorizontalAlignment="Right" Margin="0,189,153,0" Name="btnStop" VerticalAlignment="Top" Width="75" />
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation(0,100,new Duration(TimeSpan.FromSeconds(5)));
da.RepeatBehavior = RepeatBehavior.Forever; //永久迴圈
da.AutoReverse = true; //來回往復
//第一種方式
//rect.BeginAnimation(Rectangle.WidthProperty, da);
//第二種方式,可以新增多個動畫
Storyboard sb = new Storyboard();
Storyboard.SetTargetProperty(da,new PropertyPath(Rectangle.WidthProperty));
sb.Children.Add(da);
sb.Begin(rect);
}
}
相關文章
- Android備忘錄《幀動畫》Android動畫
- Android備忘錄《View動畫(補間動畫)》AndroidView動畫
- WPF備忘錄六(資料繫結篇)
- Android備忘錄《屬性動畫-ValueAnimator》Android動畫
- 備忘錄
- 【備忘錄】
- 備忘錄四:Spring Boot + P6SpySpring Boot
- 撤銷功能的實現——備忘錄模式(四)模式
- 菜鳥初學Java的備忘錄(四) (轉)Java
- 備忘錄模式模式
- iOS備忘錄iOS
- JUnit備忘錄
- CSS備忘錄CSS
- RabbitMQ備忘錄MQ
- Python 備忘錄Python
- Java備忘錄《集合》Java
- 網站備忘錄網站
- rman命令備忘錄
- Eigen備忘錄
- iOS設計模式之四:備忘錄模式和命令模式iOS設計模式
- 備忘錄模式(Memento)模式
- Java備忘錄《“==” 和 “equals”》Java
- Dart 學習備忘錄Dart
- linux 備忘記錄Linux
- JavaMementoPattern(備忘錄模式)Java模式
- 19_備忘錄模式模式
- 開發 備忘錄 - 雜
- 設計模式----備忘錄模式設計模式
- [譯] Flutter 佈局備忘錄Flutter
- Docker部署禪道備忘錄Docker
- 常用工具備忘錄
- 第 22 章 備忘錄模式模式
- 萌新(我)的Git備忘錄Git
- 設計模式 - 備忘錄模式設計模式
- Backgroundcheck:Google隱私備忘錄Go
- 初學Java的備忘錄Java
- 運維指令碼備忘錄運維指令碼
- 實驗 20:備忘錄模式模式