Opacity的TextBlock动画未按预期工作

时间:2017-06-27 13:51:59

标签: c# animation uwp

我有一个UWP应用项目,我正在尝试添加几个DoubleAnimation,我正在使用下面的代码:

private static void CreateStoryboardAnimation(StackPanel sp, ItemHelper item, EnumHelper.AddRemoveFavorites favType)
    {

        var image = (Image)sp.FindName("ImageView");
        var tb = (TextBlock)sp.FindName("FavStatusTB");
        if (image != null)
        {
            image.RenderTransform = new CompositeTransform();
            //tb.RenderTransform = new CompositeTransform();

            Storyboard tbStory = new Storyboard();
            var tbAnimateOpacity = new DoubleAnimation()
            {
                From = 1.0,
                To = 0.0,
                Duration = new Duration(TimeSpan.FromMilliseconds(500)),
            };
            Storyboard.SetTarget(tbAnimateOpacity, tb);
            Storyboard.SetTargetProperty(tbAnimateOpacity, "Opacity");
            tbStory.Children.Add(tbAnimateOpacity);

            Storyboard storyboard1 = new Storyboard();
            storyboard1.Completed += async delegate
            {
                // set text
                if (favType == EnumHelper.AddRemoveFavorites.Add)
                {
                    tb.Text = "Added to favorites";
                }
                else
                {
                    tb.Text = "Removed from favorites";
                }
                await Task.Delay(500);
                // run 2nd animation
                var storyboard2 = new Storyboard();
                var translateYAnimation2 = new DoubleAnimation()
                {
                    From = -20,
                    To = 0,
                    Duration = new Duration(TimeSpan.FromMilliseconds(700)),
                };
                Storyboard.SetTarget(translateYAnimation2, image);
                Storyboard.SetTargetProperty(translateYAnimation2, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
                storyboard2.Children.Add(translateYAnimation2);
                storyboard2.Begin();
                tbStory.Begin();
            };
            DoubleAnimation translateYAnimation = new DoubleAnimation()
            {
                From = 0,
                To = -20,
                Duration = new Duration(TimeSpan.FromMilliseconds(500))
            };
            Storyboard.SetTarget(translateYAnimation, image);
            Storyboard.SetTargetProperty(translateYAnimation, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
            storyboard1.Children.Add(translateYAnimation);

            storyboard1.Begin();
        }
    }

我第一次运行动画时效果很好,但之后就不行了。此代码将应用于GridView项,如下所示:

Image 1

图像动画工作正常,它上升然后TextBlock动画运行不佳。它应该显示500毫秒的文本,但它显示文本,然后启动动画使不透明度变为零。

我希望用户可以看到此文本至少500毫秒,然后动画应该开始。有什么东西我错过了吗?我也试过BeginTime的{​​{1}}属性,但无济于事。请分享您的建议。感谢

1 个答案:

答案 0 :(得分:0)

我认为问题在于您在第一个“上滑”动画的已完成处理程序中启动不透明度动画。这是第一次有效,因为Opacity的{​​{1}}是TextBlock,但在第一轮之后它是1.0并且仅在0.0之后翻转到1.0 await Task.Delay(500);完成。最简单的解决方法是在if

之后立即设置不透明度
if (image != null)
{
   tb.Opacity = 1; //add this line
   ...