12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace DocTools.WPF
- {
- /// <summary>
- /// WaitingSpinner.xaml 的交互逻辑
- /// </summary>
- public partial class WaitingSpinner : UserControl
- {
- #region Private Variables
- private readonly DispatcherTimer _animationTimer;
- #endregion Private Variables
- #region Constructor
- public WaitingSpinner()
- {
- InitializeComponent();
- // 每40 ms 转一下
- _animationTimer = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher) { Interval = new TimeSpan(0, 0, 0, 0, 80) };
- Visibility = Visibility.Collapsed;
- }
- #endregion Constructor
- private void Start()
- {
- _animationTimer.Tick += HandleAnimationTick;
- _animationTimer.Start();
- }
- private void Stop()
- {
- _animationTimer.Stop();
- _animationTimer.Tick -= HandleAnimationTick;
- }
- private void HandleAnimationTick(object sender, EventArgs e)
- {
- SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
- }
- private void OnCanvasLoaded(object sender, RoutedEventArgs e)
- {
- SpinnerRotate.CenterX = (float)0.5 * CanvasSpinner.Width;
- SpinnerRotate.CenterY = (float)0.5 * CanvasSpinner.Height;
- const double offset = Math.PI;
- const double step = Math.PI * 2 / 10.0;
- SetPosition(C0, offset, 0.0, step);
- SetPosition(C1, offset, 1.0, step);
- SetPosition(C2, offset, 2.0, step);
- SetPosition(C3, offset, 3.0, step);
- SetPosition(C4, offset, 4.0, step);
- SetPosition(C5, offset, 5.0, step);
- SetPosition(C6, offset, 6.0, step);
- SetPosition(C7, offset, 7.0, step);
- SetPosition(C8, offset, 8.0, step);
- }
- private void SetPosition(Ellipse ellipse, double offset, double posOffSet, double step)
- {
- ellipse.SetValue(Canvas.LeftProperty, CanvasSpinner.Width * 0.5 + Math.Sin(offset + posOffSet * step) * CanvasSpinner.Width * 0.4 - 0.5 * ellipse.Width);
- ellipse.SetValue(Canvas.TopProperty, CanvasSpinner.Width * 0.5 + Math.Cos(offset + posOffSet * step) * CanvasSpinner.Width * 0.4 - 0.5 * ellipse.Width);
- }
- private void OnCanvasUnLoaded(object sender, RoutedEventArgs e)
- {
- Stop();
- }
- /// <summary>
- /// Start the waiting animation
- /// </summary>
- public void StartWaiting()
- {
- Visibility = Visibility.Visible;
- Start();
- }
- /// <summary>
- /// Stop the waiting animation
- /// </summary>
- public void StopWaiting()
- {
- Stop();
- Visibility = Visibility.Collapsed;
- }
- }
- }
|