在动画完成时删除FrameworkElements [英] Removing FrameworkElements on Animation Completion

查看:104
本文介绍了在动画完成时删除FrameworkElements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过Sells / Griffiths的 Programming WPF自学WPF,我发现它是一个很好的资源,但是我正在尝试采用他们向我介绍的一些概念并再往前走,我就如何将各个部分组合在一起以完成我想做的事情遇到了概念上的障碍。

I've been teaching myself WPF through Sells/Griffiths' "Programming WPF", and I've found it a great resource, but I'm trying to take some of the concepts they've introduced me to and go a step further, and I'm running into a conceptual snag on how to put the pieces together to accomplish what I'm trying to do.

在本练习中,我m试图创建自我终止的动画; FrameworkElement 由事件创建,执行动画,然后删除自己。我在弄清楚如何从animation.Completed事件中调用父 FrameworkElement 时遇到问题。

In this exercise, I'm trying to create self-terminating animations; FrameworkElements that are created by Events, perform an animation, and then delete themselves. I'm having problems figuring out how to call back to the parent FrameworkElement from the animation.Completed event.

我最初问这个问题的原因是只使用了不包含在内的 DoubleAnimation ,而不是 Storyboard 的一部分。此后,我添加了 Storyboard ,并制作了 Storyboard 和矩形资源,以便可以轻松地重用它们。

I asked this question originally just using DoubleAnimations that were uncontained and not part of the Storyboard. I have since added the Storyboard, and made the Storyboard and rectangle resources so they can be reused easily.

这是我到目前为止的内容:

.xaml:

Here's what I have so far:
.xaml:

<Window.Resources>
    <Storyboard x:Key="GrowSquare" x:Shared="False">
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)" By="-50" Duration="0:0:2"/>
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" By="-50" Duration="0:0:2"/>
        <DoubleAnimation Storyboard.TargetProperty="(Ellipse.Width)" By="100" Duration="0:0:2"/>
        <DoubleAnimation Storyboard.TargetProperty="(Ellipse.Height)" By="100" Duration="0:0:2"/>
    </Storyboard>
    <Rectangle x:Key="MyRect" x:Shared="False" Width="20" Height="20">
    </Rectangle>
</Window.Resources>
<Canvas x:Name="myCanvas" MouseMove="myCanvas_MouseMove" Background="White"/>

.cs:

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            lastFire = DateTime.Now;
        }

        DateTime lastFire;

        private void myCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            DateTime nowTime = DateTime.Now;
            TimeSpan T = nowTime.Subtract(lastFire);

            if (T.TotalMilliseconds > 200)
            {
                lastFire = nowTime;
                Random Rand = new Random();

                Rectangle myRect = (Rectangle)FindResource("MyRect");
                myRect.Fill = new SolidColorBrush(Color.FromRgb((byte)Rand.Next(256), (byte)Rand.Next(256), (byte)Rand.Next(256)));
                Point myLoc = e.GetPosition(myCanvas);
                Canvas.SetLeft(myRect, myLoc.X - 10);
                Canvas.SetTop(myRect, myLoc.Y - 10);
                myCanvas.Children.Add(myRect);

                Storyboard SB = (Storyboard)FindResource("GrowSquare");
                SB.Completed += new EventHandler(SB_Completed);
                SB.Begin(myRect);
            }

        }

        void SB_Completed(object sender, EventArgs e)
        {
            myCanvas.Children.RemoveAt(0);
        }
    }

这可行,但不符合我的要求喜欢它。由于画布是空的,并且所有动画的长度都相同,因此当动画结束时,它将始终是在画布的第一个子对象上调用的动画。

This works, but not in the way I'd like it. Since the canvas is empty, and all animations are the same length, when an animation finishes, it will always be the one that was called on the first child of the canvas.

但是,我想实现需要花费随机时间的动画,这意味着动画不一定总是以相同的顺序开始和结束。在SB_Completed事件中,我想以某种方式访问​​被调用的控件,但似乎找不到该控件的路径。

However, I'd like to implement animaitons that take a random amount of time, meaning that animations will not always start and end in the same order. Somehow in the SB_Completed event, I'd like to access the control that it was being called upon, but I can't seem to find the path to it yet.

是否有一种方法可以从Media.Animation.ClockGroup中调用SB_Completed事件的控件获取到正在调用动画的控件?

Is there a way to get from the Media.Animation.ClockGroup that calls the SB_Completed event to the control that the animation is being called on?

推荐答案

更改将事件处理程序分配给的行:

change the line where you assign the event handler to this:

SB.Completed += (s,e) => myCanvas.Children.Remove(myRect);

这篇关于在动画完成时删除FrameworkElements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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