【问题标题】:Restarting a WPF Storyboard重新启动 WPF 故事板
【发布时间】:2010-10-24 08:44:53
【问题描述】:

从 .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>

所以 textone 中的文本会运行,如果你关闭屏幕并快速返回屏幕,texttwo 实际上会在新启动的故事板上播放。因此,即使我已将其移除并停止,原始(来自第一个屏幕)故事板仍然存在并正在播放。

【问题讨论】:

  • 也许这是我的设置。这是更多信息。我有一个带有 storyOverlay 故事板属性的基类。在子类的 ctor 中,我将 storyOverlay = TryFindResource("storyId") as Storyboard;然后,我正在检查 storyOVerlay != null; { storyOverlay.Remove(this); } 这有什么改变吗?

标签: wpf storyboard


【解决方案1】:

使用 Storyboard.Seek(TimeSpan.Zero) 怎么样?类似于在 Stream 中搜索,这应该会让您回到动画的开头。

我评论说,您还应该确保 IsControllable 属性设置为 true。请记住这一点!

Storyboard.Seek method

【讨论】:

  • 另外,需要确保 IsControllable 设置为 true 并标记了相应的包含对象。
【解决方案2】:

您需要在调用 myStory.Begin(this) 之前执行 myStory.Remove(this) 以使其从头开始。这是因为调用 Storyboard::Stop 只会停止动画时钟,但会保持原样。随后对 Begin 的调用将仅充当简历。我同意这有点违反直觉,但是如果您阅读文档ClockController::Stop,您会在备注中看到以下内容:

这个方法改变了目标时钟的 CurrentState 停止。

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

【讨论】:

  • 这仍然不适合我。在我同时使用 myStory.Remove(this) 和 myStory.Stop(this) 之后,如果场景重新出现在屏幕上,在故事板完成之前,最后一个(最好称之为 Original)故事板仍在播放。
【解决方案3】:

抱歉,斯科特,我没注意。您是否尝试在情节提要上设置 FillBehavior。将 FillBehavior 设置为 Stop 会重置动画。不知道为什么 Stop 不这样做...

【讨论】:

    【解决方案4】:
    <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();
    

    【讨论】:

      【解决方案5】:

      其他细节可以是:

      this.MyAnimation.FillBehavior = FillBehavior.Stop;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 2017-03-02
        • 2016-07-02
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        • 2017-04-28
        相关资源
        最近更新 更多