rendering-wpf-architecture
2
总安装量
1
周安装量
#72289
全站排名
安装命令
npx skills add https://github.com/christian289/dotnet-with-claudecode --skill rendering-wpf-architecture
Agent 安装分布
amp
1
cline
1
opencode
1
cursor
1
continue
1
kimi-cli
1
Skill 文档
WPF Rendering Architecture
Rendering Pipeline Overview
User Input â Property Change â InvalidateXxx()
â
âââââââââââââââââ
â Measure Pass â â Determines DesiredSize
âââââââââââââââââ¤
â Arrange Pass â â Determines ActualSize/Position
âââââââââââââââââ¤
â Render Pass â â OnRender() / DrawingContext
âââââââââââââââââ
â
Composition Tree â MILCore â DirectX â GPU
1. Layout Passes
Invalidation Methods
| Method | Triggers | Use When |
|---|---|---|
InvalidateMeasure() |
Measure + Arrange + Render | Size might change |
InvalidateArrange() |
Arrange + Render | Position might change |
InvalidateVisual() |
Render only | Visual appearance change |
FrameworkPropertyMetadata Flags
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double), typeof(MyControl),
new FrameworkPropertyMetadata(10.0,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));
| Flag | Effect |
|---|---|
AffectsMeasure |
Triggers Measure pass |
AffectsArrange |
Triggers Arrange pass |
AffectsRender |
Triggers Render pass |
AffectsParentMeasure |
Parent re-measures |
AffectsParentArrange |
Parent re-arranges |
Custom Control Layout
protected override Size MeasureOverride(Size availableSize)
{
// Calculate desired size based on content
double width = Math.Min(200, availableSize.Width);
double height = Math.Min(100, availableSize.Height);
return new Size(width, height);
}
protected override Size ArrangeOverride(Size finalSize)
{
// Position children within finalSize
foreach (UIElement child in Children)
{
child.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
}
return finalSize;
}
protected override void OnRender(DrawingContext dc)
{
dc.DrawRectangle(Background, null, new Rect(RenderSize));
}
2. Hardware Acceleration
Rendering Tiers
| Tier | GPU Memory | Features |
|---|---|---|
| 0 | < 30MB | Software rendering |
| 1 | 30-120MB | Partial hardware (no PS 2.0) |
| 2 | ⥠120MB | Full hardware acceleration |
int tier = RenderCapability.Tier >> 16;
// 0 = Software, 1 = Partial, 2 = Full GPU
Software Fallback Conditions
WPF falls back to software rendering when:
- GPU memory insufficient
- Texture > 8192px
- Remote Desktop (RDP) session
AllowsTransparency="True"on Window- Legacy
BitmapEffectused
RenderOptions Optimization
// For images that will be scaled
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.LowQuality);
// For pixel-perfect lines
RenderOptions.SetEdgeMode(element, EdgeMode.Aliased);
// For tiled brushes
RenderOptions.SetCachingHint(brush, CachingHint.Cache);
RenderOptions.SetCacheInvalidationThresholdMinimum(brush, 0.5);
RenderOptions.SetCacheInvalidationThresholdMaximum(brush, 2.0);
3. Performance Optimization
Batch Updates Pattern
// Bad: Multiple layout passes
for (int i = 0; i < 100; i++)
{
items[i].Width = newWidths[i]; // Each triggers layout
}
// Good: Single layout pass
using (Dispatcher.DisableProcessing())
{
for (int i = 0; i < 100; i++)
{
items[i].Width = newWidths[i];
}
} // Layout happens once here
Common Performance Issues
| Problem | Cause | Solution |
|---|---|---|
| Slow scrolling | Deep Visual Tree | Virtualization |
| Layout thrashing | Frequent InvalidateMeasure | Batch updates |
| Slow startup | Complex Grid | Simplify layout |
| Memory growth | BitmapEffect | Use Effect class |
4. Tier-Adaptive Rendering
public void ConfigureForTier()
{
int tier = RenderCapability.Tier >> 16;
if (tier < 2)
{
// Disable expensive effects
DisableDropShadows();
DisableAnimations();
UseSimplifiedVisuals();
}
}