我想遍历现有应用程序中的一些图纸(重新架构是一个没有吸引力的选择)。绘图存储为 DrawingVisual 对象:
foreach (DrawingVisual visual in TheDrawings)
{
DrawingGroup group = visual.Drawing; // this is incredibly slow
...
}
我遇到的问题是DrawingVisual.Drawing
属性获取器非常慢。仅迭代一万左右的视觉效果(不对它们做任何事情,只是迭代)需要将近一秒钟,所有时间都花在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 的子项?