I have a System.Windows.Controls.Canvas on to which I programatically place a System.Windows.Shapes.Polygon:
private Canvas mMainCanvas = new Canvas();
private Polygon mHistogram = new Polygon();
Later on, I update the polygon with a very large number of events (~1,000,000 or so). I have been trying to make this as fast and efficient as possible (a struggle in its self). My latest attempt was to accumulate the values into a PointCollection and periodically re-set the collection of the Polygon (mHistogram):
int i = 10000;
PointCollection mPc = new PointCollection(256);
double y;
Point p;
private void EventProcessor( int bin ) {
if (0 < i--) {
p = mPc[bin];
y = p.Y + 1;
p.X = p.X;
p.Y = y;
mPc[bin] = p;
if (mMainCanvas.Height < p.Y)
mMainCanvas.Height = p.Y;
}
else {
i = 10000;
mHistogram.Points = new PointCollection( mPc ); /* This works if mPc
is a PointCollection.
It does not work if
mPc is a Point[]
}
}
This seems to work OK, albeit still not fast enough. So, I changed the type of mPc from a PointCollection to a simple array of Points (Point[]), hoping this would make access a little faster. However, when I do this, my Polygon (mHistogram) fails to update at all.
This is baffling to me. I create a new PointCollection from an IEnumerable (mPc) which should create a new PointCollection that behaves just like any other PointCollection. Why does it beha开发者_如何学JAVAve differently when created with an Array (Point[])?
Thanks.
The problem you describe seems unlikely and is probably related to another issue with your code. Looking at this simple test, you can verify that the same collections are in fact created:
PointCollection collection1 = new PointCollection(new Point[] { new Point(1, 1), new Point(2, 2) });
PointCollection collection2 = new PointCollection(collection1);
for (int i = 0; i < collection2.Count; i++) {
if (collection1[i] != collection2[i])
throw new InvalidOperationException();
}
As for the assignment to the Points property, the Polygon wouldn't care how the PointsCollection was created.
Both these pieces of code produce the same results:
poly.Points = new PointCollection(new Point[] {
new Point(100, 100),
new Point(200, 200)
});
and:
var coll = new PointCollection();
coll.Add(new Point(100, 100));
coll.Add(new Point(200, 200));
poly.Points = new PointCollection(coll);
Keep in mind that the following are different:
var collection = new PointCollection(256);
var array = new Point[256];
The collection contains 0 items, but has space to hold 256 preallocated. The array contains 256 items and will only ever have space to hold 256 items.
精彩评论