LoadingMask.xaml.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. namespace SonopostSearchTool
  17. {
  18. /// <summary>
  19. /// LoadingMask.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class LoadingMask : UserControl
  22. {
  23. private readonly DispatcherTimer animationTimer;
  24. public LoadingMask()
  25. {
  26. InitializeComponent();
  27. animationTimer = new DispatcherTimer(DispatcherPriority.ContextIdle, Dispatcher);
  28. animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 90);
  29. }
  30. private void Start()
  31. {
  32. animationTimer.Tick += HandleAnimationTick;
  33. animationTimer.Start();
  34. }
  35. private void Stop()
  36. {
  37. animationTimer.Stop();
  38. animationTimer.Tick -= HandleAnimationTick;
  39. }
  40. private void HandleAnimationTick(object sender, EventArgs e)
  41. {
  42. SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
  43. }
  44. private void HandleLoaded(object sender, RoutedEventArgs e)
  45. {
  46. const double offset = Math.PI;
  47. const double step = Math.PI * 2 / 10.0;
  48. SetPosition(C0, offset, 0.0, step);
  49. SetPosition(C1, offset, 1.0, step);
  50. SetPosition(C2, offset, 2.0, step);
  51. SetPosition(C3, offset, 3.0, step);
  52. SetPosition(C4, offset, 4.0, step);
  53. SetPosition(C5, offset, 5.0, step);
  54. SetPosition(C6, offset, 6.0, step);
  55. SetPosition(C7, offset, 7.0, step);
  56. SetPosition(C8, offset, 8.0, step);
  57. }
  58. private void SetPosition(Ellipse ellipse, double offset, double posOffSet, double step)
  59. {
  60. ellipse.SetValue(Canvas.LeftProperty, 50.0 + Math.Sin(offset + posOffSet * step) * 50.0);
  61. ellipse.SetValue(Canvas.TopProperty, 50 + Math.Cos(offset + posOffSet * step) * 50.0);
  62. }
  63. private void HandleUnloaded(object sender, RoutedEventArgs e)
  64. {
  65. Stop();
  66. }
  67. private void HandleVisibleChanged(object sender,DependencyPropertyChangedEventArgs e)
  68. {
  69. bool isVisible = (bool)e.NewValue;
  70. if (isVisible)
  71. Start();
  72. else
  73. Stop();
  74. }
  75. }
  76. }