Agent Skills: Rendering Anti-Patterns

WPF rendering anti-patterns: no InvalidateVisual in loops, no resource allocation in OnRender.

UncategorizedID: christian289/dotnet-with-claudecode/wpf-rule-rendering-antipatterns

Install this agent skill to your local

pnpm dlx add-skill https://github.com/christian289/dotnet-with-claudecode/tree/HEAD/wpf-dev-pack/skills/wpf-rule-rendering-antipatterns

Skill Files

Browse the full folder contents for wpf-rule-rendering-antipatterns.

Download Skill

Loading file tree…

wpf-dev-pack/skills/wpf-rule-rendering-antipatterns/SKILL.md

Skill Metadata

Name
wpf-rule-rendering-antipatterns
Description
"WPF rendering anti-patterns: no InvalidateVisual in loops, no resource allocation in OnRender."

Rendering Anti-Patterns

Common WPF rendering mistakes that cause performance problems or visual corruption.


Anti-Pattern 1: InvalidateVisual() in a Loop

Calling InvalidateVisual() inside a loop schedules a separate render pass for each iteration. WPF does not coalesce these — each call queues a layout/render cycle, causing frame drops or freezes.

// Prohibited: InvalidateVisual inside a loop
foreach (var item in items)
{
    item.Value = ComputeNewValue(item);
    InvalidateVisual();   // ← queues N render passes
}

Correct: Batch Update Pattern

Mutate all state first, then invalidate once after the loop completes.

// Correct: one invalidation after all state is updated
foreach (var item in items)
{
    item.Value = ComputeNewValue(item);
}
InvalidateVisual();   // ← single render pass

For data-driven scenarios where items notify individually, suppress notifications during bulk update and raise a single reset at the end:

// Correct: bulk update with deferred notification
_suppressNotifications = true;
try
{
    foreach (var item in items)
        item.Value = ComputeNewValue(item);
}
finally
{
    _suppressNotifications = false;
    InvalidateVisual();
}

Anti-Pattern 2: Creating Resources in OnRender

OnRender is called on every render pass (layout changes, animations, window resize). Allocating Brush, Pen, Geometry, or other objects inside OnRender creates garbage on every frame.

// Prohibited: resource allocation inside OnRender
protected override void OnRender(DrawingContext dc)
{
    var brush = new SolidColorBrush(Colors.Red);   // ← allocated every frame
    var pen   = new Pen(brush, 1.0);               // ← allocated every frame
    dc.DrawEllipse(brush, pen, _center, _rx, _ry);
}

Move resource creation to the constructor or a property setter. See freezable-performance.md for the correct constructor-initialize-and-freeze pattern.