我将故事板动画定义为资源。我使用StoryBoard.Begin
方法启动它。我想在代码后面得到故事板动画(双重动画(的当前值。我尝试使用Changed事件,但它似乎毫无意义。如何在代码后面获得插值,双动画的当前值?
我想要的和这个问题一样:获取双动画的当前值
这是启动动画的代码:
protected override void OnMouseEnter(MouseEventArgs e)
{
speedLine = Template.FindName(speedLineName, this) as Line;
var std = speedLine.Resources["std"] as Storyboard;
var doubleAnim = std.Children[0] as DoubleAnimation;
doubleAnim.From = 220;
doubleAnim.Changed += DoubleAnim_Changed;
doubleAnim.To = MaxSpeed;
std.Begin();
base.OnMouseEnter(e);
}
以下是相关的XAML文件:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Speedometar}">
<Grid Background="{TemplateBinding Background}" x:Name="RootGrid">
<Ellipse x:Name="PART_OuterElipse" Fill="LightGray" Width="220" Height="220" />
<Ellipse Fill="Gray" Width="200" Height="200" />
<Line x:Name="PART_SpeedLine" Stroke="Red" Width="200" StrokeThickness="2" Panel.ZIndex="1" X1="100" Y1="10" X2="100" Y2="100">
<Line.RenderTransform>
<RotateTransform Angle="220" CenterX="100" CenterY="100" />
</Line.RenderTransform>
<Line.Resources>
<Storyboard x:Key="std">
<DoubleAnimation
Storyboard.TargetName="PART_SpeedLine"
Storyboard.TargetProperty="(Line.RenderTransform).(RotateTransform.Angle)"
Duration="0:0:1"
To="{StaticResource AngleResource}">
<DoubleAnimation.EasingFunction >
<CircleEase></CircleEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</Line.Resources>
</Line>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您可以使用DependencyPropertyDescriptor
:获取正在设置动画的依赖属性的当前值
RotateTransform rotateTransform = speedLine.RenderTransform as RotateTransform;
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor
.FromProperty(RotateTransform.AngleProperty, typeof(RotateTransform));
if (dpd != null)
{
dpd.AddValueChanged(rotateTransform, new EventHandler((ss, ee) =>
{
Debug.WriteLine("Current value is " + rotateTransform.Angle.ToString());
}));
}