wpf - 在确切的关键帧停止 Storyboard

标签 wpf storyboard keyframe

我为我正在制作的一些游戏制作了一个骰子(在 c# 中),它是一个用户控件,它使用 Storyboard依次显示多个图像(如幻灯片),因此它看起来像一个滚动的 3D 骰子。问题是在特定的关键帧处启动和停止它。为此使用 Pause() 和 Resume() 似乎合乎逻辑,但我无法弄清楚如何在确切的关键帧处暂停。

有些人使用单独的 dispatcherTimer 来做到这一点,但这还不够精确,无法在那个确切的关​​键帧处停止。 (例如,如果您抛出 4,它必须停在 4 图像上)。

所以,如果有这样的方法就好了:

TimeSpan keyTime = new TimeSpan(0,0,0,0,750); // 750 miliseconds
myStoryBoard.ResumeTo(keyTime); // <- doesn't exist as far as I know

这是我在 XAML 中的 Storyboard的片段:
<Storyboard x:Key="DieStoryBoard" RepeatBehavior="Forever">

        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image1">

            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.05">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>


        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image2">

            <DiscreteObjectKeyFrame KeyTime="0:0:0.05">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.10">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>


        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image3">

            <DiscreteObjectKeyFrame KeyTime="0:0:0.10">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

            <DiscreteObjectKeyFrame KeyTime="0:0:0.15">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Collapsed</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>

        </ObjectAnimationUsingKeyFrames>
.....

还有一些图片让事情更清楚:

1 2 3 4

最佳答案

尝试这个...

我的例子是一个旋转箭头,我可以将它停在指定的角度。

<Window.Resources>
    <Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames     
            Storyboard.TargetProperty="(UIElement.RenderTransform).
            (TransformGroup.Children)[2].(RotateTransform.Angle)" 
            Storyboard.TargetName="RightPanelButton1">
            <DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="45.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="90.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:3" Value="135.0"/>
            <DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="180.0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>



Storyboard st = ((Storyboard)this.Resources["Storyboard1"]);

st.Begin();
st.Seek(new TimeSpan(0,0,2));
st.Pause();

Abbas Ahmed

关于wpf - 在确切的关键帧停止 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965262/

相关文章:

iOS5 - 如何使用storyboard从UITableViewController转换到另一个ViewController

ios - Uitableviewcell 具有自定义 View ,在应用程序运行时无法正确调整大小(下图)

jquery - 通过切换类在高度之间转换?

c# - 正确调整 wpf ListView 最后一列的大小

c# - BindingOperations.GetBindingExpression 在 WPF 中返回 null

ios - 如何在 iOS 中为通用应用程序创建通用 Storyboard?

ffmpeg - 使用 FFMPEG 提取第一个非黑色关键帧

css - 在 CSS 中重新创建 MS Windows Phone Tiltin 动画

c# - 在使用 XAML Islands 的 WPF 应用程序中托管 UWP 控件的 Windows 10 真正最低支持版本是什么?

WPF : Is it impossible to UI load in background thread?