我有这样的代码
<Storyboard x:Key="AdvMarquee" Completed="Storyboard_Completed">
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="-25" To="0" BeginTime="0:00:00" Duration="0:00:01" />
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="0" To="25" BeginTime="0:00:03" Duration="0:00:01" />
</Storyboard>
<Style x:Key="AnimationImageStyle" TargetType="StackPanel">
<Setter Property="Canvas.Top" Value="200" />
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource AdvMarquee}"/>
</EventTrigger>
</Style.Triggers>
</Style>
并使用此代码应用此动画样式
<Canvas x:Name="Advertise" Background="{x:Null}" Margin="10,0,0,0" >
<StackPanel Style="{StaticResource AnimationImageStyle}">
<Button Click="Advertise_Click" Style="{StaticResource AdvertisementBtnStyle}">
<TextBlock Name="AdvText" Text="This is Animated Text" Padding="10, 0, 10, 0"/>
</Button>
</StackPanel>
</Canvas>
我尝试使用 Storyboard 上的 Completed Event 来计算 Storyboard 动画执行了多少次。
在此之前,我尝试在 Storyboard 上添加 RepeatBehavior="Forever"但它只是永远循环并且没有运行完成的事件。
现在,当我删除 RepeatBehavior="Forever"时,它完成了它的进度,计数,但它不会再次运行。
我该如何解决这个问题?
仍然不知道,因为我对使用 xaml wpfform 很陌生。
我的 Storyboard_Completed 就是这样。
int count = 0;
private void Storyboard_Completed( object sender, EventArgs e )
{
count++;
}
最佳答案
为最后一个动画放置 CurrentStateInvalidated
的事件处理程序,您将有可能获得当前迭代:
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" From="0" To="25" BeginTime="0:00:03" Duration="0:00:01" CurrentStateInvalidated="DoubleAnimation_CurrentStateInvalidated"/>
int cnt=0;
private void DoubleAnimation_CurrentStateInvalidated(object sender, EventArgs e)
{
var ac = sender as AnimationClock;
cnt = (ac.Parent as ClockGroup).CurrentIteration;
}
Storyboard_Completed
你不需要。
关于c# - 如何计算 Storyboard动画循环了多少次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52867740/