If you don't need hit testing, context menus, tooltips for your curves, you can use simple visuals instead of framework elements.
A:
Mart
2010-07-07 06:00:17
Thanks! But it is on a Canvas, so it at least must be a UIElement (to be on a panel). And since the property changes invalidate the render, the easiest way to do this is via FrameworkPropertyMetadataOptions.AffectsRender , which requires a FraworkElement. Anyway, how would this help solve the above problem?
Robert Fraser
2010-07-07 06:07:13
+3
A:
1- I would try to use StreamGeometry:
StreamGeometry streamGeo = new StreamGeometry();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
streamGeo.Clear();
var ctx = streamGeo.Open();
ctx.BeginFigure(new Point(0, 0), false, false);
ctx.QuadraticBezierTo(new Point(10, 10), new Point(10, i), true, true);
ctx.Close();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); // For 10k it took 30 ms
It looks much faster than PathGeometry+PathFigure.
When you set the Point for the QuadraticBezierSegment it recalculates everything. That's why it is slow. And more slow when it is already added to a geometry.
2- Try to use only 1 frameworkelement for all of your curves. Check this: Writing More Efficient ItemsControls
Mart
2010-07-07 07:03:36
A:
I would imagine that your performance issues come from descending from FrameworkElement
and having the WPF layout engine recompute the layout as the curve is computed.
What you can consider is to model the curve by descending from Freezable
and then use a FrameworkElement
(like PathGeometry) to display the actual geometry.
codekaizen
2010-07-07 07:03:43
The layout is actually only recomputed when either the source or destination changes, but if the user is dragging a node, this can be every time the mouse moves times the number of affected paths. The path is not frozen, so it cannot descend from freezable.
Robert Fraser
2010-07-07 07:15:13
Freezable doesn't mean you can't change it - you just can't change it after it is frozen. I'd imagine it to be possible to copy, mutate the curve, freeze the geometry and update the FrameworkElement that contains it much faster than if you make a FrameworkElement do it all. Ultimately, I just think there will be no way to get acceptable performance from using FrameworkElement as a base.
codekaizen
2010-07-07 07:24:13