向后查找故事板动画到特定点然后播放
我有一个简单的故事板动画,如下所示:
<Window.Resources>
<Storyboard x:Key="fader">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:4" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
如您所见,它使目标淡入,等待 3.5 秒,然后淡出。我在事件发生时出现的通知程序窗口上使用它。
当该事件发生时,它可以通过从代码中调用以下内容来完美地工作:
board = (Storyboard)FindResource("fader");
board.Begin(this);
但是,我正在尝试处理通知窗口已经显示时发生第二个事件的情况。在这种情况下,我正在寻找的行为是修改窗口的内容以反映第二个事件(简单)并将故事板“倒带”到其生命周期中的 0.5 秒,即。就在它出现之后。
换句话说,第二个事件应该导致在第二个事件发生后 3.5 秒发生淡出,即使窗口已经存在了 0 到 6 秒之间的时间。
我尝试通过检测窗口的不透明度来做到这一点,如果它> 0,则调用以下命令:
board.Seek(this,TimeSpan.FromSeconds(0.5),TimeSeekOrigin.BeginTime);
但是,这似乎没有执行任何操作,即使我反复向其触发事件,窗口内容更改,但通知程序会尽职尽责地淡入,等待 3.5 秒,然后再次淡出。
为什么这不起作用?这是实现我想要实现的目标的最佳方法吗?
I've got a simple storyboard animation that looks like this:
<Window.Resources>
<Storyboard x:Key="fader">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:4" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
As you can see, it fades the target in, waits 3.5 seconds, then fades it out. I'm using it on a notifier window that appears when an event occurs.
It works perfectly by calling the following from code, when that event happens:
board = (Storyboard)FindResource("fader");
board.Begin(this);
However, I'm trying to handle the case where a second event occurs while the notification window is already showing. In this case, the behaviour I'm looking for is to modify the contents of the window to reflect the second event (easy) and 'rewind' the storyboard to 0.5s through it's lifetime, ie. just after it's appeared.
In other words, the second event should cause the fadeout to occur 3.5s after the second event has occurred, even if the window has already been around for anything between 0 and 6 seconds.
I've tried to do this by detecting the opacity of the window, and if it's >0 then calling the following:
board.Seek(this,TimeSpan.FromSeconds(0.5),TimeSeekOrigin.BeginTime);
However, this doesn't appear to do anything, and even if I fire events at it repeatedly, the window contents change, but the notifier dutifully fades in, waits 3.5 seconds and then fades out again.
Why does't this work, and is this the best way of doing what I'm trying to achieve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里的问题是你将淡入和淡出作为单个动画。我可能会做类似以下的事情:
displayPeriod
常数值 3.5s(或者任何你想要的。用户可配置的设置?)。displayPeriod
秒数进行倒计时。有更多代码需要编写,但这可能比尝试将动画填充到故事板中的正确点更简单。
如果您想在实现此功能时使用一些很酷且令人费解的技术,请查看 Reactive用于处理通知和计时器事件流的扩展。
I think the problem here is that you have the fade in and fade out as a single animation. I would probably do something like the following:
displayPeriod
constant value of 3.5s (or whatever you want. User-configurable setting?).displayPeriod
number of seconds.There's more code to write, but it's possibly simpler than trying to shim the animation to the right point in the storyboard.
If you want some cool and mind-bending tech to play with whilst implementing this, check out the Reactive Extensions to handle your streams of notification and timer events.