说明:
1. Storyboard是一种控制时间播放动画的常用方法。 Storyboard 要有Name, 比如clockStoryboard( <Storyboard x:Name="clockStoryboard"/>), 可以使用Name来控制动画的播放与暂停等操作, 如在 Grid的Loaded事件中让动画开始(<Grid Loaded="AnimationStart"/>...在cs文件中定义事件public void AnimationStart(object sender, EventArgs e){ clockStoryboard.Begin();})。
2. Storyboard中有多种Animation, 如 DoubleAnimation、 ColorAnimation、 PointAnimation。
3. Storyboard中最重要的两个属性是 Storyboard.TargetName 和 Storyboard.TargetProperty。 其中, TargetProperty说明要在该目标的哪一个属性上做动画,比如在Ellipse的color属性上做动画,那么Storyboard.TargetName="ellipseName" Storyboard.TargetProperty="(Fill).(Color)", 本例中Angle属性是RenderTransform的属性值。
4. 本例是在Ellipse的RenderTransform上做动画, 使Ellipse绕着中心点转动, 其中ellipse是1分钟转180度
5. Duration="时: 分:秒" 说明需要多长时间完成一个动画周期。
代码:
MainPage.xaml:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="SilverlightApplication2.MainPage"
Width="640" Height="480" mc:Ignorable="d">
<UserControl.Resources>
<Storyboard x:Name="clockStoryboard">
<DoubleAnimation x:Name="ellipseAnimation" Storyboard.TargetName="ellipseTransform" Storyboard.TargetProperty="Angle" Duration="0:1:0" RepeatBehavior="Forever" To="180"/>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Black" >
<Canvas Width="500" Height="400" Background="Beige" Loaded="SetAndStartClock"></Canvas>
<Ellipse x:Name="ellipse" Stroke="Black" Height="80" VerticalAlignment="Bottom" Opacity="0.3" RenderTransformOrigin="3.788,0.838" Margin="0,0,0,65" HorizontalAlignment="Left" Width="80" d:LayoutOverrides="Width">
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FFFA1B14" Offset="1"/>
</LinearGradientBrush>
</Ellipse.Fill>
<Ellipse.RenderTransform>
<RotateTransform x:Name="ellipseTransform"/>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</UserControl>
MainPage.xaml.cs:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
public void SetAndStartClock(object sender, EventArgs e)
{
clockStoryboard.Begin();
}
}
}