如何禁止动画WPF? [英] How to stop an animation WPF?

查看:175
本文介绍了如何禁止动画WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁止动画,这样就不会产生已完成事件。下面是简单的例子

How to stop an animation so it won't produce Completed event. Here's simple example

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="248" Width="318">
    <Grid>
        <Border Width="20" Height="20" Background="Red" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave" x:Name="border" />
    </Grid>
</Window>

和后盾code:

private void Border_MouseEnter(object sender, MouseEventArgs e)
{
    var a = new DoubleAnimation { To = 0, Duration = TimeSpan.FromMilliseconds(4000) };
    a.Completed += (obj, args) => MessageBox.Show("Boom!");
    border.BeginAnimation(Border.OpacityProperty, a);
}

private void Border_MouseLeave(object sender, MouseEventArgs e)
{
    border.BeginAnimation(Border.OpacityProperty, null);
    border.Opacity = 1;
}

如果我将鼠标移动之前的矩形变为白色它仍然会显示弹出一段时间后窗口。如何prevent呢?让我们假设 Border_MouseLeave Border_MouseEnter 方法,不知道对方(他们无法通过动画实例变量对方)。

If I move mouse out before rectangle becomes white it still will display popup window after some time. How to prevent this? Let's assume that Border_MouseLeave and Border_MouseEnter methods doesn't know about each other (they can't pass animation instance variable to each other).

推荐答案

您可以使用此:

<Border Width="20" Height="20" Background="Red" x:Name="border" >
                <Border.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard Name="Ali">
                            <Storyboard>
                                <DoubleAnimation To="0" Duration="0:0:4" Completed="com" Storyboard.TargetProperty="Opacity"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <StopStoryboard  BeginStoryboardName="Ali"/>
                    </EventTrigger>
                </Border.Triggers>
            </Border>

private void com(object sender, EventArgs e)
        {
            MessageBox.Show("boom!");
        }

这篇关于如何禁止动画WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆