使用摇动效果左右移动 WPF 窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17434877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:08:08  来源:igfitidea点击:

Animate a WPF Window left and right with a shake effect?

c#wpfanimationwpf-controls

提问by darbid

Could someone please show me how to animate a window from its current position. I am looking for a shake effect which simply shakes the window left and right say 5 to 6 times.

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

I understand that I need to use Animation.By. This is something I have started but am not getting far.

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

This However does not work.

但是这不起作用。

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

I have managed to get the right shake effect but I cannot do it from the windows current position.

我设法获得了正确的抖动效果,但我无法从 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 >

All help would be appreciated.

所有帮助将不胜感激。

回答by Ali M

Set the Left property of your window to 500 and add this code:

将窗口的 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>

You should set the property Leftof Windowmanually when you don't mention From="x"otherwise it set to Auto and when you try to shake your window the value of Left is NaN and an exception will be thrown.

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

回答by Erno

You could use a BounceEaseto make the window shake:

您可以使用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>