从.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实际上会播放一个新启动的故事板。所以原版(从第一个屏幕开始)故事板仍然在播放,即使我已经删除,并停止它。

有帮助吗?

解决方案

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

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

Storyboard.Seek方法

其他提示

对不起斯科特,我没有注意。您是否尝试在故事板上设置FillBehavior。将FillBehavior设置为Stop以重置动画。不知道为什么Stop不会这样做......

<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.please试试这个

storyboard_name.Remove();

其他细节可以是:

this.MyAnimation.FillBehavior = FillBehavior.Stop;

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top