maui-graphics-drawing

使用 Microsoft.Maui.Graphics 进行自定义绘图的指导,涵盖 GraphicsView 与画布绘制操作、形状绘制、路径绘制、文本渲染、图像绘制、阴影效果、裁剪以及画布状态管理。适用于“自定义绘图”、“GraphicsView”、“画布绘制”、“绘制形状”、“绘制路径”、“绘制文本”、“ICanvas”、“IDrawable”、“阴影”、“裁剪”、“Microsoft.Maui.Graphics”。不适用于视图动画(请使用 maui-animations)、绘制元素的触控手势处理(请使用 maui-gestures)或应用图标(请使用 maui-app-icons-splash)。

仓库最近推送
GitHub Stars
181
Forks
21
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "maui-graphics-drawing":https://skillpi.com/zh/skills/deepskill-222bd6a114e2d1723c99bbe25f98cb6e09053f0040bda51495f008a67be0ea20。安装前核对上游来源和版本:https://github.com/davidortinau/maui-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

使用 Microsoft.Maui.Graphics 进行自定义绘图的指导,涵盖 GraphicsView 与画布绘制操作、形状绘制、路径绘制、文本渲染、图像绘制、阴影效果、裁剪以及画布状态管理。适用于“自定义绘图”、“GraphicsView”、“画布绘制”、“绘制形状”、“绘制路径”、“绘制文本”、“ICanvas”、“IDrawable”、“阴影”、“裁剪”、“Microsoft.Maui.Graphics”。不适用于视图动画(请使用 maui-animations)、绘制元素的触控手势处理(请使用 maui-gestures)或应用图标(请使用 maui-app-icons-splash)。

0 下载

介绍来源: third_party · davidortinau · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: maui-graphics-drawing
description: >
  Guidance for custom drawing with Microsoft.Maui.Graphics, GraphicsView, canvas drawing operations,
  shapes, paths, text rendering, image drawing, shadows, clipping, and canvas state management.
  USE FOR: "custom drawing", "GraphicsView", "canvas drawing", "draw shapes", "draw path",
  "draw text", "ICanvas", "IDrawable", "shadows", "clipping", "Microsoft.Maui.Graphics".
  DO NOT USE FOR: view animations (use maui-animations), gesture handling on drawn elements
  (use maui-gestures), or app icons (use maui-app-icons-splash).

.NET MAUI Graphics Drawing

Common gotchas

Issue Fix
Nothing draws on screen Ensure Drawable is set on GraphicsView and the control has non-zero HeightRequest/WidthRequest
State bleeds between shapes Wrap isolated sections in SaveState() / RestoreState() pairs
Shadows stick to later draws Call canvas.SetShadow(SizeF.Zero, 0, null) after drawing the shadowed element
Clipping never resets Clipping is cumulative per frame — use SaveState/RestoreState around clip regions
UI freezes during drawing Never do I/O, network, or heavy computation inside Draw() — it runs on the UI thread

Canvas state — always pair Save/Restore

⚠️ Unpaired SaveState/RestoreState causes state leaks across draw calls.

// ✅ Correct — isolated state
canvas.SaveState();
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
canvas.RestoreState();
// Stroke reverts to previous values

// ❌ Wrong — state leaks to everything drawn after
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
// Every subsequent shape is now red with size 6

Saves/restores: stroke, fill, font, shadow, clip, and transforms. Nest calls for layered isolation.

Triggering redraws

// ✅ Correct — queue a redraw
graphicsView.Invalidate();

// ❌ Wrong — never call Draw() directly
myDrawable.Draw(canvas, rect);
  • Invalidate() queues a redraw; the framework calls IDrawable.Draw on the next frame.
  • ⚠️ Avoid calling Invalidate() in a tight loop — batch state changes, then invalidate once.

Performance tips

  • Keep Draw() fast — pre-compute paths and data outside the draw method; Draw() is called on every frame.
  • Reuse PathF objects — create them once, store as fields, draw repeatedly.
  • Use MeasureFirstItem-style thinking — if drawing many identical items, calculate dimensions once.
  • Minimize allocations — avoid new PathF() inside Draw() when the path doesn't change.
// ✅ Pre-computed path (field)
private readonly PathF _starPath = BuildStarPath();

public void Draw(ICanvas canvas, RectF dirtyRect)
{
    canvas.FillPath(_starPath);
}

// ❌ Allocating every frame
public void Draw(ICanvas canvas, RectF dirtyRect)
{
    var path = new PathF();
    // ...build path every frame...
    canvas.FillPath(path);
}

Shadows and clipping — sticky state pitfalls

Shadows apply to all subsequent draws until explicitly removed. Clips accumulate and can only be undone with RestoreState():

// ✅ Shadow: remove after use
canvas.SetShadow(new SizeF(5, 5), 4, Colors.Gray);
canvas.FillRectangle(20, 20, 100, 60);
canvas.SetShadow(SizeF.Zero, 0, null); // ← must remove

// ✅ Clip: isolate with SaveState/RestoreState
canvas.SaveState();
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillRectangle(0, 0, 200, 200); // clipped
canvas.RestoreState();                  // clip removed

// ❌ Clip persists — everything after is also clipped
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillEllipse(150, 150, 50, 50); // unintentionally clipped!

Set properties BEFORE draw calls

// ✅ Properties then draw
canvas.StrokeColor = Colors.Blue;
canvas.DrawRectangle(10, 10, 100, 50);

// ❌ Setting after draw has no effect on previous shape
canvas.DrawRectangle(10, 10, 100, 50);
canvas.StrokeColor = Colors.Blue;

Decision framework

Need Approach
Simple shapes / static graphics Single IDrawable, draw in Draw()
Animated graphics Update state externally, call Invalidate() from timer/animation
Complex layered scene Multiple SaveState/RestoreState blocks, or separate drawables
Hit testing on drawn elements Track shape bounds manually — GraphicsView has no built-in hit test on drawn content
Platform-specific rendering Use handlers/platform code; Microsoft.Maui.Graphics is cross-platform only

Quick checklist

  • [ ] GraphicsView has Drawable set and non-zero size
  • [ ] Draw() is fast — no I/O, no heavy allocations
  • [ ] Every SaveState() has a matching RestoreState()
  • [ ] Shadows removed with SetShadow(SizeF.Zero, 0, null) after use
  • [ ] Clips wrapped in SaveState/RestoreState blocks
  • [ ] Properties set before the draw call they apply to
  • [ ] Invalidate() used instead of calling Draw() directly

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值75/100

亮点

  • 提供 .NET MAUI Graphics 绘图的实质性技术指导
  • 覆盖常见陷阱和性能优化建议
  • 面向开发者解决实际绘图问题
触发精准95/100

亮点

  • 触发词覆盖完整且边界清晰
  • 明确区分适用和不适用场景
  • 技术术语准确
结构规范90/100

亮点

  • 章节结构清晰完整
  • 包含代码示例和决策框架
  • 提供实用的检查清单
执行可靠95/100

亮点

  • 代码示例语法正确
  • 覆盖主要异常情况和最佳实践
  • 提供完整的避坑指南
证据可验 (前四项均值)89/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。