3

有人可以告诉我如何从当前位置为窗口设置动画。我正在寻找一种摇动效果,它只是左右摇动窗口 5 到 6 次。

我知道我需要使用 Animation.By。这是我已经开始但还没有走远的事情。

然而这不起作用。

<Storyboard x:Key="sbShake1" FillBehavior="Stop">
    <DoubleAnimation Storyboard.TargetName="W1" Storyboard.TargetProperty ="(Window.Left)"
                     By="10" Duration="0:0:1">
    </DoubleAnimation >
</Storyboard >

我设法获得了正确的抖动效果,但我无法从 windows 当前位置做到这一点。

<Storyboard x:Key="sbShake" RepeatBehavior ="00:00:01" SpeedRatio ="25" >
    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty ="Left">
        <SplineDoubleKeyFrame KeyTime ="00:00:00.1000000" Value ="-10"/>
        <SplineDoubleKeyFrame KeyTime ="00:00:00.3000000" Value ="0"/>
        <SplineDoubleKeyFrame KeyTime ="00:00:00.5000000" Value ="10"/>
        <SplineDoubleKeyFrame KeyTime ="00:00:00.7000000" Value ="0"/>
    </DoubleAnimationUsingKeyFrames >
</Storyboard >

所有帮助将不胜感激。

4

2 回答 2

6

将窗口的 Left 属性设置为 500 并添加以下代码:

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.MouseDown" >
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard TargetProperty="Left">
                    <DoubleAnimation From="500" To="515" Duration="0:0:0.05"
                                     AutoReverse="True" RepeatBehavior="3x"
                                     FillBehavior="Stop"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>

当您没有提及时,您应该手动设置属性Left,否则将其设置为 Auto,并且当您尝试摇动窗口时,Left 的值是 NaN,并且将引发异常。WindowFrom="x"

于 2014-07-30T09:02:30.180 回答
1

您可以使用BounceEase使窗口抖动:

<Storyboard x:Name="myStoryboard">
    <DoubleAnimation By="10" Duration="00:00:3"
                     AutoReverse="True" RepeatBehavior="1"
                     Storyboard.TargetName="W1" 
                     Storyboard.TargetProperty="Left">
        <DoubleAnimation.EasingFunction>
            <BounceEase Bounces="2" EasingMode="EaseOut" 
                        Bounciness="2" />
        </DoubleAnimation.EasingFunction>
    </DoubleAnimation>
</Storyboard>
于 2013-07-02T20:21:09.967 回答