Exemplo n.º 1
0
        /// <summary>
        /// 设置圆的位置和旋转角度
        /// </summary>
        private void SetEclipsePosition(ProgressBarDataModel dataModel)
        {
            //圆周长就是:C = π * d 或者C=2*π*r(其中d是圆的直径,r是圆的半径)
            double r = dataModel.R;

            var    children = ProgressBarCanvas.Children;
            int    count    = children.Count;
            double step     = (Math.PI * 2) / count;

            //根据圆中正弦、余弦计算距离
            int index = 0;

            foreach (var element in children)
            {
                var ellipse = element as Ellipse;
                //透明度
                var opacity = Convert.ToDouble(index) / (count - 1);
                ellipse.SetValue(UIElement.OpacityProperty, opacity < 0.05 ? 0.05 : opacity);
                //距离
                double left = r + Math.Sin(step * index) * r;
                ellipse.SetValue(Canvas.LeftProperty, left);
                double top = r - Math.Cos(step * index) * r;
                ellipse.SetValue(Canvas.TopProperty, top);

                index++;
            }
        }
        /// <summary>
        /// 加载后刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProgressBarControl_OnLoaded(object sender, RoutedEventArgs e)
        {
            animationTimer = new DispatcherTimer(DispatcherPriority.ContextIdle, Dispatcher);

            //指定时间间隔
            animationTimer.Interval = new TimeSpan(0, 0, 0, 0, TimeSpan);

            if (EllipseCount < 1)
            {
                EllipseCount = 12;
            }

            ProgressBarCanvas.Children.Clear();

            for (int i = 0; i < EllipseCount; i++)
            {
                ProgressBarCanvas.Children.Add(new Ellipse());
            }
            var dataModel = new ProgressBarDataModel()
            {
                CanvasSize  = CanvasSize,
                EclipseSize = EllipseSize
            };

            _dataModel       = dataModel;
            this.DataContext = dataModel;
        }