繁体   English   中英

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

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

我正在尝试为GradientBrush创建一个动画,该动画将为每个渐变设置动画,以无限期地循环遍历渐变中包含的所有颜色。

由于这项任务的复杂性,我已经开始处理代码中的所有内容,而不是XAML。

幸运的是,我的代码没有错误或异常。

不幸的是,它也一无所获。

符合最小,完整和可验证的示例要求:

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;
            }
        }
    }
}

我已经尝试仅对ColorAnimationUsingKeyFrames中的每个渐变使用ColorAnimationUsingKeyFrames ,并且DOES有效,但我更喜欢使用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