Hi,
It takes 5-6 seconds with the following modifications:
var now = DateTime.Now;
diagram.ValidityChecks = false;
diagram.BeginInit();
for (int i = 0; i <= 1024; i++)
{
ShapeNode srcNode = createNode(new Rect(100, 100 + i * 40, nodeWidth, nodeHeight));
ShapeNode snkNode = createNode(new Rect(400, 100 + i * 40, nodeWidth, nodeHeight));
DiagramLink link1 = new DiagramLink(diagram, srcNode, snkNode);
diagram.Links.Add(link1);
}
diagram.EndInit();
diagram.LinkCrossings = LinkCrossings.Cut;
Debug.WriteLine(DateTime.Now - now);
where diagram.LinkCrossings is moved from InitDiagram. And the createNode method is refactored to use shared BitmapImage and ShadowEffect objects:
BitmapImage encoderImage = new BitmapImage(
new Uri(@"..\..\Encoder.png", UriKind.RelativeOrAbsolute));
Color shadowColor = new Color { ScA = 1, ScB = 0, ScG = 0, ScR = 0 };
DropShadowEffect dropShadow = new DropShadowEffect {
RenderingBias = RenderingBias.Performance, ShadowDepth = 8 };
private ShapeNode createNode(Rect rect)
{
var mainNode = new ShapeNode { Bounds = rect };
diagram.Nodes.Add(mainNode);
// ...
mainNode.Image = encoderImage;
// ...
var lr = new Rect(mainNode.Bounds.X - mainNode.Bounds.Width, mainNode.Bounds.Y,
mainNode.Bounds.Width, mainNode.Bounds.Height);
var lableNode = new ShapeNode { Bounds = lr };
diagram.Nodes.Add(lableNode);
lableNode.AttachTo(mainNode, AttachToNode.MiddleLeft);
// ...
mainNode.Effect = dropShadow;
return mainNode;
}
There was an unnecessary Nodes.Add(mainNode) at the end of the method, now remove. Watch out for adding items two times, especially with ValidityChecks disabled.
I hope that helps,
Stoyan