c# - Storyboard动画停不下来,我只能开始

标签 c# wpf storyboard wpf-animation doubleanimation

我正在尝试使用 Storyboard管理动画,它是一个闪烁的标签,为了实现这种效果,我使用了这段代码(对于动画,我更喜欢只在代码后面工作): 我宣布我的 Storyboard;

    public partial class Example: Page
    {
         public Storyboard LabelStoryboard=new Storyboard(); 
    }

然后我启动动画:

 private void FlashWinLabel(){

        DoubleAnimation Flash= new DoubleAnimation();
        Flash.Duration = TimeSpan.FromMilliseconds(400);
        Flash.From = 1.0d;
        Flash.To = 0.0d;
        Flash.RepeatBehavior = RepeatBehavior.Forever;
        WinLabel.BeginAnimation(Label.OpacityProperty, Flash);

        SolidColorBrush myAnimatedBrush = new SolidColorBrush();
        myAnimatedBrush.Color = Colors.White;
        WinLabel.Foreground = myAnimatedBrush;


        this.RegisterName("MyAnimatedBrush", myAnimatedBrush);

        ColorAnimation LabelColorAnimation = new ColorAnimation();
        LabelColorAnimation.To = Colors.GreenYellow;
        LabelColorAnimation.Duration = TimeSpan.FromMilliseconds(200);
        LabelColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
        LabelColorAnimation.AutoReverse = true;
        Storyboard.SetTargetName(LabelColorAnimation, "MyAnimatedBrush");
        Storyboard.SetTargetProperty(
            LabelColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));

        LabelStoryboard.Children.Add(LabelColorAnimation);

        WinLabel.BeginStoryboard(LabelStoryboard);


    }

到此为止一切正常,当我尝试通过按屏幕上的按钮停止 Storyboard 时出现问题:

     private void StopStoryBoard()
    {
        LabelStoryboard.Stop();

    }

LabelStoryboard 不为空,che code .Stop() 被执行但没有停止动画。我的代码有什么问题?

最佳答案

问题不在Stop(),而在Begin()

要使 Storyboard在代码中可控,您必须使用 Storyboard的 Begin 方法的适当重载并指定 true 以使其可控。

冗长的 msdn 示例:

namespace Microsoft.Samples.Animation.TimingBehaviors
{
    public partial class ControlStoryboardExample : Page
    {    
        private Storyboard myStoryboard;

        public ControlStoryboardExample()
        {        
            // Create a name scope for the page.
            NameScope.SetNameScope(this, new NameScope());        

            this.WindowTitle = "Controlling a Storyboard";
            this.Background = Brushes.White;

            StackPanel myStackPanel = new StackPanel();
            myStackPanel.Margin = new Thickness(20);

            // Create a rectangle.
            Rectangle myRectangle = new Rectangle();
            myRectangle.Width = 100;
            myRectangle.Height = 20;
            myRectangle.Margin = new Thickness(12,0,0,5);
            myRectangle.Fill = new SolidColorBrush(Color.FromArgb(170, 51, 51, 255));
            myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
            myStackPanel.Children.Add(myRectangle);

            // Assign the rectangle a name by 
            // registering it with the page, so that
            // it can be targeted by storyboard
            // animations.
            this.RegisterName("myRectangle", myRectangle);           

            //
            // Create an animation and a storyboard to animate the
            // rectangle.
            //
            DoubleAnimation myDoubleAnimation = 
                new DoubleAnimation(100, 500, new Duration(TimeSpan.FromSeconds(5)));            
            Storyboard.SetTargetName(myDoubleAnimation, "myRectangle");
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));
            myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);

            //
            // Create some buttons to control the storyboard
            // and a panel to contain them.
            //
            StackPanel buttonPanel = new StackPanel();
            buttonPanel.Orientation = Orientation.Horizontal;
            Button beginButton = new Button();
            beginButton.Content = "Begin";
            beginButton.Click += new RoutedEventHandler(beginButton_Clicked);            
            buttonPanel.Children.Add(beginButton);
            Button pauseButton = new Button();
            pauseButton.Content = "Pause";
            pauseButton.Click +=new RoutedEventHandler(pauseButton_Clicked);
            buttonPanel.Children.Add(pauseButton);
            Button resumeButton = new Button();
            resumeButton.Content = "Resume";
            resumeButton.Click +=new RoutedEventHandler(resumeButton_Clicked);
            buttonPanel.Children.Add(resumeButton);
            Button skipToFillButton = new Button();
            skipToFillButton.Content = "Skip to Fill";
            skipToFillButton.Click +=new RoutedEventHandler(skipToFillButton_Clicked);
            buttonPanel.Children.Add(skipToFillButton);
            Button setSpeedRatioButton = new Button();
            setSpeedRatioButton.Content = "Triple Speed";
            setSpeedRatioButton.Click +=new RoutedEventHandler(setSpeedRatioButton_Clicked);
            buttonPanel.Children.Add(setSpeedRatioButton);
            Button stopButton = new Button();
            stopButton.Content = "Stop";
            stopButton.Click +=new RoutedEventHandler(stopButton_Clicked);
            buttonPanel.Children.Add(stopButton);
            myStackPanel.Children.Add(buttonPanel);           
            this.Content = myStackPanel;            
        }

        // Begins the storyboard.
        private void beginButton_Clicked(object sender, RoutedEventArgs args)
        {
            // Specifying "true" as the second Begin parameter
            // makes this storyboard controllable.
            myStoryboard.Begin(this, true);  
        }

        // Pauses the storyboard.
        private void pauseButton_Clicked(object sender, RoutedEventArgs args)
        {
             myStoryboard.Pause(this); 
        }

        // Resumes the storyboard.
        private void resumeButton_Clicked(object sender, RoutedEventArgs args)
        {
             myStoryboard.Resume(this);                  
        }     

        // Advances the storyboard to its fill period.
        private void skipToFillButton_Clicked(object sender, RoutedEventArgs args)
        {
             myStoryboard.SkipToFill(this);                  
        } 

        // Updates the storyboard's speed.
        private void setSpeedRatioButton_Clicked(object sender, RoutedEventArgs args)
        {
            // Makes the storyboard progress three times as fast as normal.
            myStoryboard.SetSpeedRatio(this, 3);                  
        }           

        // Stops the storyboard.
        private void stopButton_Clicked(object sender, RoutedEventArgs args)
        {
             myStoryboard.Stop(this);          

        }         
    }
}

关于c# - Storyboard动画停不下来,我只能开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22960370/

相关文章:

c# - 在 Xamarin.form 上访问 SQLite 数据库的最简单方法是什么?

c# - 提取数据库特定 id :s with the repository pattern?

c# - 如何在 C# WPF 中选中/取消选中列表框中的所有复选框?

c# - 大数据表如何保存?

objective-c - 如何在 xib 上设置静态 UITableview?

c# - 如何构建数据模型以绑定(bind)到具有 XML 列的 SQL 表

c# - 将滚动条添加到文本框

iphone - 将数据从 plist 加载到 UITableView

ios - 关于如何在 iOS 中创建编码用户界面的提示

c# - 在可变长度数组之间复制 c#