0

我正在尝试创建动画,但在动画发生后执行某些代码时遇到问题。动画代码如下...

public static void Friend(Canvas canvas)
{
    foreach (var element in canvas.Children.OfType<Image>())
    {
        var elementName = Regex.Split(element.Name, "_");
        if (elementName[0] == "friend")
        {
            var slideDown = new DoubleAnimation
                {
                    From = Canvas.GetBottom(element),
                    To = Canvas.GetBottom(element) - element.Height,
                    Duration = new Duration(TimeSpan.FromSeconds(1)),
                    AutoReverse = true
                };
            element.BeginAnimation(Canvas.BottomProperty, slideDown);
            slideDown.Completed += (sender, e) => Test(sender, e, element, canvas);
        }
    }
}

之后发生的事件是……

var random = new Random();
element.Source = Core.StreamImage(Wardrobe.Friends[Wardrobe.RandomFriend()]);
Canvas.SetLeft(element, random.Next(0, (int)(canvas.ActualWidth - element.Width)));
// then reverse the previous animation.

如您所见,事件需要保留动画的上下文才能使其工作,但事件处理程序不允许这样做。我曾尝试直接在下面添加此代码,element.BeginAnimation但它会过早执行。

我还尝试将动画的各个部分拆分为单独的函数,但当然你不能在一个对象上拥有多个动画,因为这些函数不会按顺序执行,导致只播放最后一部分。

4

2 回答 2

0

尝试将事件移到执行之上

public static void Friend(Canvas canvas)
{
    foreach (var element in canvas.Children.OfType<Image>())
    {
        var elementName = Regex.Split(element.Name, "_");
        if (elementName[0] == "friend")
        {
            var slideDown = new DoubleAnimation
                {
                    From = Canvas.GetBottom(element),
                    To = Canvas.GetBottom(element) - element.Height,
                    Duration = new Duration(TimeSpan.FromSeconds(1)),
                    AutoReverse = true
                };
            slideDown.Completed += (sender, e) => Test(sender, e, element, canvas);
            element.BeginAnimation(Canvas.BottomProperty, slideDown);
        }
    }
}

我不是 100% 但我认为动画在执行后会变成 Freezable,所以我们不能修改

顺便说一句,您可以在一个对象上有多个动画,但您需要使用“故事板”。

故事板示例:

 var slideDown = new DoubleAnimation { From = 100, To = 200, Duration = new Duration(TimeSpan.FromSeconds(5)), AutoReverse = true };
 var slideLeft = new DoubleAnimation { From = 100, To = 200, BeginTime = TimeSpan.FromSeconds(5), Duration = new Duration(TimeSpan.FromSeconds(5)), AutoReverse = true };

 Storyboard storyBoard = new Storyboard();
 storyBoard.Children.Add(slideDown);
 storyBoard.Children.Add(slideLeft);
 Storyboard.SetTargetName(slideDown, myCanvas.Name);
 Storyboard.SetTargetName(slideLeft, myCanvas.Name);
 Storyboard.SetTargetProperty(slideLeft, new PropertyPath(Canvas.LeftProperty));
 Storyboard.SetTargetProperty(slideDown, new PropertyPath(Canvas.TopProperty));

 myCanvas.BeginStoryboard(storyBoard);
于 2012-11-28T03:32:13.373 回答
0

这是我用来解决我遇到的问题的代码......

public static void Friend(Canvas canvas)
{
    var random = new Random();
    foreach (var element in canvas.Children.OfType<Image>())
    {
        var elementName = Regex.Split(element.Name, "_");
        if (elementName[0] == "friend")
        {
            var slideDown = new DoubleAnimation
            {
                From = Canvas.GetBottom(element),
                To = Canvas.GetBottom(element) - element.Height,
                Duration = new Duration(TimeSpan.FromSeconds(1))
            };
            slideDown.Completed += (sender, e) =>
            {
                element.Source = Core.StreamImage(Wardrobe.Friends[Wardrobe.RandomFriend()]);
                Canvas.SetLeft(element, random.Next(0, (int)(canvas.ActualWidth - element.Width)));
                var slideUp = new DoubleAnimation
                {
                    From = Canvas.GetBottom(element),
                    To = Canvas.GetBottom(element) + element.Height,
                    Duration = new Duration(TimeSpan.FromSeconds(1))
                };
                element.BeginAnimation(Canvas.BottomProperty, slideUp);
            };
            element.BeginAnimation(Canvas.BottomProperty, slideDown);
        }
    }
}
于 2012-11-28T03:44:12.960 回答