我为我正在制作的一些游戏制作了一个骰子(在 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>
.....

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

最佳答案

试试这个...

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

<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/

10-10 01:31