uwp win2d 使用

Fork Me On Github
zodream 编程技术 C# 2021年05月

通过 Canvas.Invalidate(); 触发重绘事件

通过 Canvas_Draw 进行绘制

c#
                                 
        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);
        }
123456789101112131415161718192021222324252627282930313233

最后必须手动销毁

c#
     
var canvas = GetTemplateChild("Canvas") as CanvasControl;
if (canvas != null)
{
    canvas.RemoveFromVisualTree();
}
12345
点击查看全文
标签: uwp
0 374 0
UWP Custom Control自定义控件开发
uwp应用获取应用内的文件内容
UWP 使用语言包
2020年04月
UWP 上传图片
2020年04月
UWP 怎么上传图片