我想从代码背后创建一个故事板,我想从代码背后实现下面的故事板,
<Storyboard x:Name="Counter_Animation">
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtCounter" Storyboard.TargetProperty="(TextBlock.Text)">
<DiscreteObjectKeyFrame KeyTime="00:00:01" Value="2"/>
<DiscreteObjectKeyFrame KeyTime="00:00:02" Value="1"/>
<DiscreteObjectKeyFrame KeyTime="00:00:03" Value="0"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>在我的页面上有一个名为txtCounter的TextBlock,我试图通过代码实现,如下所示,
private void CounterAnimation()
{
Storyboard storyboard = new Storyboard();
ObjectAnimationUsingKeyFrames anim = new ObjectAnimationUsingKeyFrames();
anim.BeginTime = TimeSpan.FromSeconds(0);
DiscreteObjectKeyFrame keyFrames = new DiscreteObjectKeyFrame();
keyFrames.KeyTime = TimeSpan.FromSeconds(1);
keyFrames.Value = "2";
keyFrames.KeyTime = TimeSpan.FromSeconds(2);
keyFrames.Value = "1";
keyFrames.KeyTime = TimeSpan.FromSeconds(3);
keyFrames.Value = "0";
anim.KeyFrames.Add(keyFrames);
Storyboard.SetTarget(anim, txtCounter);
Storyboard.SetTargetProperty(anim, "Text");
storyboard.Children.Add(anim);
storyboard.Begin();
}代码编译良好并运行的问题是,在关键时间1 txtCounter应该显示2,在关键时间2 txtCounter应该显示1,在关键时间3 txtCounter应该显示0,但它直接显示0(即不显示中间值)。如果我弄错了,请看一下并纠正我。这将help.Thanks
发布于 2020-04-27 21:37:47
只是在这里写完整的代码,如果有人有更好的想法,那么请建议这对我来说是可行的,
private Storyboard GetCounterAnimation()
{
Storyboard storyboard = new Storyboard();
ObjectAnimationUsingKeyFrames counterAnimation = new ObjectAnimationUsingKeyFrames
{
BeginTime = TimeSpan.FromSeconds(0),
};
counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(1), Value = "2" });
counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(2), Value = "1" });
counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(3), Value = "Inhale" });
Storyboard.SetTarget(counterAnimation, txtCounter);
Storyboard.SetTargetProperty(counterAnimation, "Text");
storyboard.Children.Add(counterAnimation);
return storyboard;
}https://stackoverflow.com/questions/61458946
复制相似问题