using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace BPASmartClient.MilkWithTea.Control { /// /// CircularProgressBar.xaml 的交互逻辑 /// public partial class CircularProgressBar : UserControl { public CircularProgressBar() { InitializeComponent(); UpdateValue(Percent * 3.6); } public double Percent { get { return (double)GetValue(PercentProperty); } set { SetValue(PercentProperty, value); UpdateValue(Percent * 3.6); } } //public static DependencyProperty PercentProperty => percentProperty; // Using a DependencyProperty as the backing store for Percent. This enables animation, styling, binding, etc... private static readonly DependencyProperty PercentProperty = DependencyProperty.Register("Percent", typeof(double), typeof(CircularProgressBar), new PropertyMetadata(default(double), new PropertyChangedCallback(OnPropertyChanged))); private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CircularProgressBar _cycleProgess = d as CircularProgressBar; _cycleProgess.UpdateValue(_cycleProgess.Percent * 3.6); } private void UpdateValue(double angle) { if (angle > 360) return; if (angle < 0) return; int offset = 5; if (angle > 359) angle = 359.8; double radius = this.Width / 2 - offset; if (radius < 0) return; double b = radius * Math.Sin(angle * Math.PI / 180); double x = radius + b; double y = radius - radius * Math.Cos(angle * Math.PI / 180); double startPositionX = radius + offset; double startPositionY = offset; Storyboard sb = new Storyboard(); DoubleAnimation yd1 = new DoubleAnimation();//实例化浮点动画 cyclePath.RenderTransform = new RotateTransform();//设置为旋转动画 cyclePath.RenderTransformOrigin = new Point(0.5, 0.5);//设置旋转的中心 yd1.From = 0;//动画的起始值 yd1.To = 360;//动画的结束值 yd1.Duration = TimeSpan.FromSeconds(3);//动画的播放时间 yd1.RepeatBehavior = RepeatBehavior.Forever;//设置动画循环播放 Storyboard.SetTarget(yd1, cyclePath);//给故事板绑定动画 Storyboard.SetTargetProperty(yd1, new PropertyPath("RenderTransform.Angle"));//动画的依赖属性 sb.Children.Add(yd1);//故事板添加动画 if (angle == 0) { 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); this.cyclePath.Data = Geometry.Parse(Data); sb.Begin();//播放动画 } else if (angle > 180) { //string Data = string.Format("M {0} 0 A {0} {0} 0 1 1 {1} {2}", radius, Math.Round(x, 2), Math.Round(y, 2)); 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); this.cyclePath.Data = Geometry.Parse(Data); sb.Stop(); } else { //string Data = string.Format("M {0} 0 A {0} {0} 0 0 1 {1} {2}", radius, Math.Round(x,2), Math.Round(y, 2)); 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); this.cyclePath.Data = Geometry.Parse(Data); sb.Stop(); } } } }