Sliding Clock Animation didn't Work

IKINGSHADOW 136 Reputation points
2020-10-18T21:52:36.253+00:00

hello everyone , I try to Make A clock Like Android Phones Sliding Clocks in Wpf But I dont know why my codes didn't work correctly ,
could you help me please :

33173-screenshot-2020-10-19-011914.jpg

33077-screenshot-2020-10-19-011931.jpg

because there was so many codes i upload the project in mega , so you can download it from link below:

https://mega.nz/file/PhpiDCIR#XxHV0T9HGIuD47sRsm8mCx4pdSRWpvJF7gqeBE4QvSs

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-10-19T08:21:41.097+00:00

    Hi,
    you cam simplified your code:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;
    
    namespace Sliding_Watch
    {
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
        double[] digits = new double[6];
    
        public MainWindow()
        {
          InitializeComponent();
          DispatcherTimer MyTimer = new DispatcherTimer();
          MyTimer.Interval = TimeSpan.FromSeconds(1);
          MyTimer.Tick += MyTimer_Tick;
          MyTimer.Start();
        }
    
    
    
        private void ClockAnimation(StackPanel MyPanel, double MyTo)
        {
          Storyboard MyStory = new Storyboard();
          ThicknessAnimation ani = new ThicknessAnimation();
          ani.From = MyPanel.Margin;
          ani.To = new Thickness(0, MyTo, 0, 0);
          ani.Duration = new Duration(TimeSpan.FromMilliseconds(100));
          Storyboard.SetTarget(ani, MyPanel);
          Storyboard.SetTargetProperty(ani, new PropertyPath("Margin"));
          MyStory.Children.Add(ani);
          MyStory.Begin();
        }
    
        private void MyTimer_Tick(object sender, EventArgs e)
        {
          double[] newdigits = GetDigits();
          if (digits[0] != newdigits[0]) ClockAnimation(StackHours01, -newdigits[0] * 150);
          if (digits[1] != newdigits[1]) ClockAnimation(StackHours02, -newdigits[1] * 150);
          if (digits[2] != newdigits[2]) ClockAnimation(StackMins01, -newdigits[2] * 150);
          if (digits[3] != newdigits[3]) ClockAnimation(StackMins02, -newdigits[3] * 150);
          if (digits[4] != newdigits[4]) ClockAnimation(StackSec01, -newdigits[4] * 150);
          if (digits[5] != newdigits[5]) ClockAnimation(StackSec02, -newdigits[5] * 150);
          digits = newdigits;
        }
    
        private double[] GetDigits()
        {
          double[] d = new double[6];
          string t = DateTime.Now.ToString("HHmmss");
          for (int i = 0; i < 6; i++) d[i] = int.Parse(t.Substring(i, 1));
          return d;
        }
    
        private void MainBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          this.DragMove();
        }
      }
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-10-19T05:24:14.207+00:00

    How about the below workaround without using Animation?
    1.Put the

     double MyHours = DateTime.Now.Hour;  
     double MyMinute = DateTime.Now.Minute;  
     double MySecond = DateTime.Now.Second;  
    

    into MyTimer_Tick, otherwise values obtained will not change
    2. Add below code to replace the code in ClockAnimation

                Thickness thick = new Thickness(0, MyTo, 0, 0);  
                MyPanel.Margin = thick;  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments