使用数组构造的 .NET PointCollection 与从 PointCollection 构造的 .NET PointCollection 的行为不同
我有一个 System.Windows.Controls.Canvas,我以编程方式在其上放置了一个 System.Windows.Shapes.Polygon:
private Canvas mMainCanvas = new Canvas();
private Polygon mHistogram = new Polygon();
稍后,我使用大量事件(约 1,000,000 个左右)更新多边形。我一直在努力使其尽可能快速和高效(这本身就是一场斗争)。我最近的尝试是将这些值累积到 PointCollection 中,并定期重新设置多边形的集合(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[]
}
}
这似乎工作正常,尽管仍然不够快。因此,我将 mPc 的类型从 PointCollection 更改为简单的点数组 (Point[]),希望这将使访问速度更快一些。但是,当我这样做时,我的多边形(mHistogram)根本无法更新。
这让我很困惑。我从 IEnumerable (mPc) 创建一个新的 PointCollection,它应该创建一个新的 PointCollection,其行为与任何其他 PointCollection 一样。为什么使用数组 (Point[]) 创建时它的行为有所不同?
谢谢。
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 behave differently when created with an Array (Point[])?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的问题似乎不太可能,并且可能与您的代码的另一个问题有关。查看这个简单的测试,您可以验证实际上是否创建了相同的集合:
对于 Points 属性的分配,Polygon 不会关心 PointsCollection 是如何创建的。
这两段代码产生相同的结果:
并且:
请记住以下几点是不同的:
集合包含 0 个项目,但有空间容纳 256 个预分配的项目。该数组包含 256 个项目,并且只能有空间容纳 256 个项目。
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:
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:
and:
Keep in mind that the following are different:
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.