WPF故事板和更新标签内容问题

时间:2014-07-10 14:01:37

标签: c# wpf storyboard wpf-controls

我有一个故事板,帮助我在两个或更多Canvas对象集合之间执行动画。在其中一个画布中,我有一个标签执行简单的倒计时更新 - 谁不工作。

当我在2 Canvas之间执行切换后,控制台向我发出警告:

我正在努力更新这个标签内容
  

System.Windows.Media.Animation警告:6:无法执行操作   因为指定的Storyboard从未应用于此对象   互动控制。行动='停止&#39 ;;   故事板=' System.Windows.Media.Animation.Storyboard&#39 ;;   Storyboard.HashCode =' 12309819&#39 ;;   Storyboard.Type =' System.Windows.Media.Animation.Storyboard&#39 ;;   TargetElement =' MyTest.MainWindow&#39 ;; TargetElement.HashCode =' 17070976&#39 ;;   TargetElement.Type =' MyTest.MainWindow'

我的Switcher for Canvas对象的代码示例是:

 public Storyboard _storyboard = new Storyboard();
        const bool ToLeft = false;
        const bool ToRight = true;
        void ScreenSelector(FrameworkElement currentElement, FrameworkElement toNextElement, bool side)
        {
            if (_storyboard != null) _storyboard.Stop(this);

            var ticknessLeft = new Thickness(Width, 0, -Width, 0);
            var ticknessRight = new Thickness(-Width, 0, Width, 0);
            var ticknessClient = new Thickness(0, 0, 0, 0);

            var timeSpanStarting = TimeSpan.FromSeconds(0);
            var timeSpanStopping = TimeSpan.FromSeconds(0.5);

            var keyTimeStarting = KeyTime.FromTimeSpan(timeSpanStarting);
            var keyTimeStopping = KeyTime.FromTimeSpan(timeSpanStopping);

            toNextElement.Margin = side ? ticknessRight : ticknessLeft;
            toNextElement.Visibility = Visibility.Visible;

            var storyboardTemp = new Storyboard();

            var currentThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames {BeginTime = timeSpanStarting};

            Storyboard.SetTargetName(currentThicknessAnimationUsingKeyFrames, currentElement.Name);
            Storyboard.SetTargetProperty(currentThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
            currentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(ticknessClient, keyTimeStarting));
            currentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(side ? ticknessLeft : ticknessRight, keyTimeStopping));

            storyboardTemp.Children.Add(currentThicknessAnimationUsingKeyFrames);

            var nextThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames {BeginTime = timeSpanStarting};

            Storyboard.SetTargetName(nextThicknessAnimationUsingKeyFrames, toNextElement.Name);
            Storyboard.SetTargetProperty(nextThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
            nextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(side ? ticknessRight : ticknessLeft, keyTimeStarting));
            nextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(ticknessClient, keyTimeStopping));

            storyboardTemp.Children.Add(nextThicknessAnimationUsingKeyFrames);

            storyboardTemp.Completed += (EventHandler)delegate
                {
                currentElement.Visibility = Visibility.Hidden;
                _storyboard = null;
            };

            _storyboard = storyboardTemp;
            BeginStoryboard(storyboardTemp);
        }

现在我有一个MainWindow_KeyDown事件,我检查按下的按钮:

    switch (e.Key)
    {
        case Key.A:
        {
            ScreenSelector(fr_GameGrid,Layer_1, ToLeft);
            GameEvents gm = new GameEvents();
            gm.OnTimer();
        }
        break;

        case Key.B:
        {
            ScreenSelector(Layer_1, fr_GameGrid, ToRight);
        }
        break;

我执行"屏幕选择器" - 并启动简单的倒计时器。在GameEvent中,我有一个简单的代码:

 public static void Countdown(int count, TimeSpan interval, Action<int> ts)
        {
            var dt = new DispatcherTimer { Interval = interval };
            dt.Tick += (_, a) =>
            {
                if (count-- == 0)
                    dt.Stop();
                else
                    ts(count);
            };
            ts(count);
            dt.Start();
        } 

和使用:

public void OnTimer()
        {
            /*
             * VALIDATE TIME
             * 
             * 
             * > OnAnswer
             */

            Countdown(3, TimeSpan.FromSeconds(1), cur => lbTime.Content = cur.ToString(CultureInfo.InvariantCulture));

        }

任何人都可以告诉我哪里做错了吗?不能启动Label动画,这很奇怪 - 因为我在第6行更新了我的故事板 - &gt; this(if(_storyboard!= null) _storyboard.Stop(this);)to(if(_storyboard!= null) _storyboard.Begin(this,true);)警告消失。

0 个答案:

没有答案