终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 rivejä
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace BPASmartClient.MilkWithTea.Control
  18. {
  19. /// <summary>
  20. /// CircularProgressBar.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class CircularProgressBar : UserControl
  23. {
  24. public CircularProgressBar()
  25. {
  26. InitializeComponent();
  27. UpdateValue(Percent * 3.6);
  28. }
  29. public double Percent
  30. {
  31. get { return (double)GetValue(PercentProperty); }
  32. set
  33. {
  34. SetValue(PercentProperty, value);
  35. UpdateValue(Percent * 3.6);
  36. }
  37. }
  38. //public static DependencyProperty PercentProperty => percentProperty;
  39. // Using a DependencyProperty as the backing store for Percent. This enables animation, styling, binding, etc...
  40. private static readonly DependencyProperty PercentProperty =
  41. DependencyProperty.Register("Percent", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(default(double), new PropertyChangedCallback(OnPropertyChanged)));
  42. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  43. {
  44. CircularProgressBar _cycleProgess = d as CircularProgressBar;
  45. _cycleProgess.UpdateValue(_cycleProgess.Percent * 3.6);
  46. }
  47. private void UpdateValue(double angle)
  48. {
  49. if (angle > 360) return;
  50. if (angle < 0) return;
  51. int offset = 5;
  52. if (angle > 359) angle = 359.8;
  53. double radius = this.Width / 2 - offset;
  54. if (radius < 0) return;
  55. double b = radius * Math.Sin(angle * Math.PI / 180);
  56. double x = radius + b;
  57. double y = radius - radius * Math.Cos(angle * Math.PI / 180);
  58. double startPositionX = radius + offset;
  59. double startPositionY = offset;
  60. Storyboard sb = new Storyboard();
  61. DoubleAnimation yd1 = new DoubleAnimation();//实例化浮点动画
  62. cyclePath.RenderTransform = new RotateTransform();//设置为旋转动画
  63. cyclePath.RenderTransformOrigin = new Point(0.5, 0.5);//设置旋转的中心
  64. yd1.From = 0;//动画的起始值
  65. yd1.To = 360;//动画的结束值
  66. yd1.Duration = TimeSpan.FromSeconds(3);//动画的播放时间
  67. yd1.RepeatBehavior = RepeatBehavior.Forever;//设置动画循环播放
  68. Storyboard.SetTarget(yd1, cyclePath);//给故事板绑定动画
  69. Storyboard.SetTargetProperty(yd1, new PropertyPath("RenderTransform.Angle"));//动画的依赖属性
  70. sb.Children.Add(yd1);//故事板添加动画
  71. if (angle == 0)
  72. {
  73. string Data = string.Format("M {0} {1} A {2} {2} 0 1 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
  74. this.cyclePath.Data = Geometry.Parse(Data);
  75. sb.Begin();//播放动画
  76. }
  77. else if (angle > 180)
  78. {
  79. //string Data = string.Format("M {0} 0 A {0} {0} 0 1 1 {1} {2}", radius, Math.Round(x, 2), Math.Round(y, 2));
  80. string Data = string.Format("M {0} {1} A {2} {2} 0 1 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
  81. this.cyclePath.Data = Geometry.Parse(Data);
  82. sb.Stop();
  83. }
  84. else
  85. {
  86. //string Data = string.Format("M {0} 0 A {0} {0} 0 0 1 {1} {2}", radius, Math.Round(x,2), Math.Round(y, 2));
  87. string Data = string.Format("M {0} {1} A {2} {2} 0 0 1 {3} {4}", startPositionX, startPositionY, radius, Math.Round(x, 2) + offset, Math.Round(y, 2) + offset);
  88. this.cyclePath.Data = Geometry.Parse(Data);
  89. sb.Stop();
  90. }
  91. }
  92. }
  93. }