简体   繁体   中英

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.

Because of the complex nature of this task, I've taken to handling everything in the code behind, rather than in 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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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