我想在一个现有的应用程序中迭代一些绘图(重新架构是一个没有吸引力的选项)。图纸存储为DrawingVisual对象:
foreach (DrawingVisual visual in TheDrawings)
{
DrawingGroup group = visual.Drawing; // this is incredibly slow
...
}
我遇到的问题是DrawingVisual.Drawing
属性getter非常慢。只需迭代一万个左右的视觉效果(没有做任何 它们,只是迭代)花费将近一秒钟,所有时间花在DrawingVisual.Drawing
上。
深入研究WPF源代码,DrawingVisual.Drawing
委托给ContainerVisual.GetDrawing
,该代码解析为实用程序类DrawingServices
中的这一昂贵调用:
internal static DrawingGroup DrawingGroupFromRenderData(RenderData renderData)
{
DrawingGroup drawingGroup = new DrawingGroup();
DrawingContext drawingContext = drawingGroup.Open();
DrawingDrawingContext drawingDrawingContext = drawingContext as DrawingDrawingContext;
if (drawingDrawingContext != null)
drawingDrawingContext.CanBeInheritanceContext = false;
DrawingContextDrawingContextWalker drawingContextWalker = new DrawingContextDrawingContextWalker(drawingContext);
renderData.BaseValueDrawingContextWalk((DrawingContextWalker) drawingContextWalker);
drawingContext.Close();
return drawingGroup;
}
有没有人知道更好(即更快)的方式来迭代DrawingVisual的子项?