通过 Canvas.Invalidate();
触发重绘事件
通过 Canvas_Draw
进行绘制
private void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
var progress = Progress;
var centerX = (float)ActualWidth / 2;
var centerY = (float)ActualHeight / 2;
var radius = Math.Min(centerX, centerY);
var lineRadius = radius - LineWidth;
var inlineRadius = lineRadius - 5;
using (var draw = args.DrawingSession)
{
draw.FillRectangle(new Windows.Foundation.Rect(centerX - radius, centerY - radius, 2 * radius, 2 * radius),
Colors.Transparent);
draw.FillCircle(centerX, centerY, inlineRadius, InlineBackground);
draw.DrawCircle(centerX, centerY, lineRadius, Outline, LineWidth);
var deg = (2 * Math.PI / 100 * progress)
; // 圆环的绘制
draw.DrawGeometry(Arc(draw, centerX, centerY, lineRadius, (float)(-.5 * Math.PI), (float)deg), Inline, LineWidth);
var x = (float)(centerX + Math.Cos(Math.PI * 2 * (progress - 25) / 100) * lineRadius);
var y = (float)(centerY + Math.Sin(Math.PI * 2 * (progress - 25) / 100) * lineRadius);
draw.FillCircle(x, y, LineWidth, Inline);
}
}
/// 画圆弧
public CanvasGeometry Arc(ICanvasResourceCreator resourceCreator, float centerX, float centerY, float radius, float startAngle, float endAngle)
{
var path = new CanvasPathBuilder(resourceCreator);
path.BeginFigure(centerX, centerY - radius);
path.AddArc(new Vector2(centerX, centerY), radius, radius, startAngle, endAngle);
path.EndFigure(CanvasFigureLoop.Open);
return CanvasGeometry.CreatePath(path);
}
最后必须手动销毁
var canvas = GetTemplateChild("Canvas") as CanvasControl;
if (canvas != null)
{
canvas.RemoveFromVisualTree();
}
转载请保留原文链接: https://www.zodream.cn/blog/id/207.html