重新启动一个WPF故事板

8

如何在 .net 代码中正确停止和重新启动一个故事板?

我正在尝试...

myStory.Stop(this);

期望下一次调用 .Begin(this); 会从时间轴的零开始重新启动,但实际上故事板从停止的地方继续运行。

我已经尝试过

.Remove(this);

我尝试过

.Seek(TimeSpan.Zero); 

这也没有起作用。

更多细节...这是我的故事板示例。

<Storyboard x:Key="overlay">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textone" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="texttwo" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

因此,当您关闭屏幕并迅速返回屏幕时,文本one中的文本会继续运行,并且文本two实际上会在一个新启动的故事板上播放。 因此,即使我已将其删除并停止播放,但最初(来自第一屏幕)的故事板仍然存在并正在播放。


也许是我的设置问题。这里有更多信息。我有一个带有storyOverlay故事板属性的基类。在子类的构造函数中,我执行storyOverlay = TryFindResource("storyId") as Storyboard;然后稍后,我检查是否storyOVerlay != null; { storyOverlay.Remove(this); }这会改变什么吗? - ScottCate
5个回答

11

尝试使用Storyboard.Seek(TimeSpan.Zero)呢?类似于在Stream中寻找,这应该会将动画带回到开头。

我评论说您还应确保IsControllable属性设置为true。记得牢记这一点!

Storyboard.Seek方法


另外,需要确保IsControllable被设置为true,并标记适当的包含对象。 - Rob

3
在调用 myStory.Begin(this) 之前,您需要执行 myStory.Remove(this),以使其从头开始。这是因为调用 Storyboard::Stop 只会停止动画时钟,但不会破坏它们。随后的 Begin 调用将仅充当恢复操作。我同意这有点反直觉,但是如果您阅读文档 ClockController::Stop,则会在备注中看到以下内容:

此方法将目标时钟的 CurrentState 更改为 Stopped。

可以使用 Begin、Seek 或 SeekAlignedToLastTick 方法重新启动已停止的时钟。


这对我仍然没有用。在我执行了myStory.Remove(this)和myStory.Stop(this)之后,如果场景在故事板完成之前重新出现在屏幕上,则最后一个(也许更好称为原始的)故事板仍在播放。 - ScottCate

0
<Storyboard x:Key="overlay">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textone" Storyboard.TargetProperty="(UIElement.Opacity)">
    <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="texttwo" Storyboard.TargetProperty="(UIElement.Opacity)">
    <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="1"/>
    <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="0"/>
</DoubleAnimationUsingKeyFrames>

 using System.Windows.Media.Animation;

然后创建新的故事板

 Storyboard storyboard_name = (Storyboard)(FindResource("overlay")); 
 storyboard_name.Begin();

故事板 "storyboard_name" 将开始。

如果你想停止故事板,请尝试这样做

storyboard_name.Stop();

如果你想删除故事板,请尝试以下方式。
storyboard_name.Remove();

0
抱歉Scott,我没有注意。你尝试在Storyboard上设置FillBehavior了吗?将FillBehavior设置为Stop会重置动画。不确定为什么Stop不能这样做...

-1

其他细节可以是:

this.MyAnimation.FillBehavior = FillBehavior.Stop;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接