gpt4 book ai didi

c# - 暂停/恢复 WPF Storyboard

转载 作者:行者123 更新时间:2023-11-30 17:50:19 28 4
gpt4 key购买 nike

我已经阅读了十几篇关于如何暂停和恢复 WPF Storyboard 的文章,但我就是无法让它发挥作用。这是我的问题:我有一个带有 Storyboard的用户控件。 Storyboard看起来像这样:

 <UserControl.Resources>
<Storyboard x:Key="TheStoryboard" RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="Arc1">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="90"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="180"/>
<EasingDoubleKeyFrame KeyTime="0:0:6" Value="270"/>
<EasingDoubleKeyFrame KeyTime="0:0:8" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>

很简单,它让方舟永远旋转。现在在后面的代码中,我有一个依赖属性,它绑定(bind)到一个 bool 值,指示动画何时应该旋转或停止。这会触发一个方法,理论上应该暂停或恢复动画。它看起来像这样:

private void SetStoryBoardActivity(bool play)
{
var storyboard = (Storyboard)this.Resources["TheStoryboard"];
if (play)
{
storyboard.Resume();
}
else
{
storyboard.Pause();
}
}

执行路径按预期进入方法,但是调用Pause()时动画没有停止;我试过了

    storyboard.Stop();
storyboard.Stop(this);
storyboard.Stop(this.Arc1);
storyboard.Freeze();
storyboard.Pause();
storyboard.Pause(this);
storyboard.Pause(this.Arc1);

但似乎没有任何效果。有谁知道我做错了什么?

最佳答案

所以在我的案例中答案似乎是视觉状态。我设法通过以下方式完成这项工作:我将 Storyboard移动到这样的可视化状态管理器:

<Grid x:Name="LayoutRoot">  
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Animation">
<VisualState x:Name="On">
<Storyboard RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="Arc1">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="90"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="180"/>
<EasingDoubleKeyFrame KeyTime="0:0:6" Value="270"/>
<EasingDoubleKeyFrame KeyTime="0:0:8" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Off"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ed:Arc x:Name="Arc1" ArcThickness="15" ArcThicknessUnit="Pixel" StartAngle="30" EndAngle="150" Fill="#C0375E77" HorizontalAlignment="Center" Height="200" Width="200" Margin="0,0,0,0" Stretch="None" Stroke="#FF204050" StrokeThickness="2" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" >
<ed:Arc.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</ed:Arc.RenderTransform>
</ed:Arc>
</Grid>

请注意,视觉状态管理器并未放置在控件的资源中,而是放置在控件的主容器中。ed 在哪里:

xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"

SetStoryBoardActivity 方法变为:

 private void SetStoryBoardActivity()
{
VisualStateManager.GoToState(this, this.AnimationActive ? "On" : "Off", true);
}

其中this.AnimationActive是我问题中提到的依赖属性。

关于c# - 暂停/恢复 WPF Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858542/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com