简体   繁体   English

为什么代码中定义的故事板不起作用?

[英]Why doesn't this storyboard defined in code work?

I'm attempting to create an animation for a GradientBrush that will animate each gradient to cycle through all of the colors contained within the gradient indefinitely. 我正在尝试为GradientBrush创建一个动画,该动画将为每个渐变设置动画,以无限期地循环遍历渐变中包含的所有颜色。

Because of the complex nature of this task, I've taken to handling everything in the code behind, rather than in XAML. 由于这项任务的复杂性,我已经开始处理代码中的所有内容,而不是XAML。

Fortunately, I'm getting no errors or exceptions with my code. 幸运的是,我的代码没有错误或异常。

Unfortunately, it's also doing absolutely nothing. 不幸的是,它也一无所获。

In compliance with the minimal, complete and verifiable example requirements: 符合最小,完整和可验证的示例要求:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace MCVE {
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class Program {
        private static  LinearGradientBrush TestBrush { get; } =
            new LinearGradientBrush(
                new GradientStopCollection( new[ ]{
                    new GradientStop( Colors.Black, 0 / 1.0D ),
                    new GradientStop( Colors.White, 1 / 1.0D )
                } ), new Point( 0.0D, 0.0D ), new Point( 1.0D, 1.0D ) );
        [STAThread]
        public static int Main( ) {
            Storyboard board = CreateStoryboard( TestBrush );
            Window w = new Window( ){
                Background = TestBrush
            };

            w.Loaded += new RoutedEventHandler(
                ( S, E ) => board.Begin( w, true ) );
            Program program = new Program( );
            program.InitializeComponent( );
            return program.Run( w );
        }

        public static Storyboard CreateStoryboard( GradientBrush brush ) {
            Storyboard board = new Storyboard( ){
                Duration = new Duration( TimeSpan.FromSeconds( 1.0D ) ),
                RepeatBehavior = RepeatBehavior.Forever
            };

            foreach (
                var animation
                in brush.GradientStops.Select(
                    GS => _CreateGradientStopAnimation(
                        GS, brush.GradientStops.SkipWhile( G => G != GS
                        ).Concat( brush.GradientStops.TakeWhile( G => G != GS )
                        ).Concat( new[ ] { GS } ).Select( G => G.Color ) ) ) )
                board.Children.Add( animation );

            return board;

            ColorAnimationUsingKeyFrames _CreateGradientStopAnimation(
                GradientStop stop, IEnumerable<Color> colors ) {
                ColorAnimationUsingKeyFrames animation =
                    new ColorAnimationUsingKeyFrames( );
                Storyboard.SetTarget( animation, stop );
                Storyboard.SetTargetProperty(
                    animation, new PropertyPath(
                        GradientStop.ColorProperty ) );

                foreach ( var keyFrame in colors.Select(
                    C => new EasingColorKeyFrame( C ) ) )
                    animation.KeyFrames.Add( keyFrame );

                return animation;
            }
        }
    }
}

I've tried using just the ColorAnimationUsingKeyFrames for each gradient within the brush, and that DOES work, but I would prefer to use a Storyboard ( if possible ) so that all of the animations can be started together. 我已经尝试仅对ColorAnimationUsingKeyFrames中的每个渐变使用ColorAnimationUsingKeyFrames ,并且DOES有效,但我更喜欢使用Storyboard (如果可能),以便所有动画可以一起启动。

Is what I am trying to do possible? 我正在尝试做什么? If so, what am I doing wrong? 如果是这样,我做错了什么?

If not, what can I do to accomplish what I am trying to get done here ( starting up many different animations simultaneously )? 如果没有,我该怎么做才能完成我想在这里完成的任务(同时启动许多不同的动画)?

Not sure what exactly the problem is, but it seems sufficient to freeze the Storyboard: 不知道究竟是什么问题,但冻结故事板似乎已经足够了:

var storyboard = CreateStoryboard(TestBrush);
storyboard.Freeze();

Loaded += (s, e) => storyboard.Begin();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM