XAML代码:
1 <Window x:Class="路径动画.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Window.Resources> 6 <!--路径资源--> 7 <PathGeometry x:Key="path"> 8 <PathFigure IsClosed="True"> 9 <ArcSegment Point="200,200" Size="30,10" SweepDirection="Clockwise"></ArcSegment> 10 <ArcSegment Point="300,200" Size="5,5"></ArcSegment> 11 </PathFigure> 12 </PathGeometry> 13 </Window.Resources> 14 <!---事件触发器,窗体加载时动画开始,周期6秒,无限循环--> 15 <Window.Triggers> 16 <EventTrigger RoutedEvent="Window.Loaded"> 17 <BeginStoryboard> 18 <Storyboard> 19 <DoubleAnimationUsingPath Storyboard.TargetName="image" Storyboard.TargetProperty="(Canvas.Left)" 20 PathGeometry="{StaticResource path}" Duration="0:0:6" RepeatBehavior="Forever" Source="X"></DoubleAnimationUsingPath> 21 <DoubleAnimationUsingPath Storyboard.TargetName="image" Storyboard.TargetProperty="(Canvas.Top)" 22 PathGeometry="{StaticResource path}" Duration="0:0:6" RepeatBehavior="Forever" Source="Y"></DoubleAnimationUsingPath> 23 </Storyboard> 24 </BeginStoryboard> 25 </EventTrigger> 26 </Window.Triggers> 27 <Canvas> 28 <!--显示路径--> 29 <Path Margin="30" Stroke="#ddd" Data="{StaticResource path}"></Path> 30 <!--动画元素--> 31 <Image Name="image" Source="123.png" Width="48" Height="48" /> 32 </Canvas> 33 </Window>
.CS代码:
1 using System.Windows; 2 3 namespace 路径动画 4 { 5 /// <summary> 6 /// MainWindow.xaml 的交互逻辑 7 /// </summary> 8 public partial class MainWindow : Window 9 { 10 public MainWindow() 11 { 12 InitializeComponent(); 13 } 14 } 15 }
时间: 2024-10-11 06:19:02