0

在我的应用程序中,我必须绘制一条包含很多点的线(如果需要,最多可达 100.000)。最初我使用的是折线,但对于大量的点来说这太慢了。

我一直在做一些阅读并来到 DrawingVisual 和 StreamGeometry 但是它不起作用。

我遵循了一个简单的教程并创建了以下类:

class ConcentratieGrafiek : FrameworkElement
{
    VisualCollection Visuals;
    PointCollection PuntenCollectie;

    public ConcentratieGrafiek(PointCollection Punten)
    {
        Visuals = new VisualCollection(this);
        PuntenCollectie = Punten;

        this.Loaded += new RoutedEventHandler(ConcentratieGrafiekLoaded);
    }

    void ConcentratieGrafiekLoaded(object sender, RoutedEventArgs e)
    {
        DrawingVisual Visual = new DrawingVisual();

        StreamGeometry g = new StreamGeometry();
        using (StreamGeometryContext cr = g.Open())
        {
            foreach (Point Punt in PuntenCollectie)
            {
                if (PuntenCollectie.IndexOf(Punt).Equals(0))
                {
                    cr.BeginFigure(Punt, false, false);
                }
                else
                {
                    cr.LineTo(Punt, false, false);
                }
            }
        }

        using (DrawingContext dc = Visual.RenderOpen())
        {
            Pen p = new Pen(Brushes.Black, 1);
            dc.DrawGeometry(Brushes.Black, p, g);
        }

        Visuals.Add(Visual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return Visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return Visuals.Count;
        }
    }
}

我需要的线的所有点都在名为 Punten 的 PointCollection 中。然后我尝试将此 FrameworkElement 添加到我的画布中。

ActieveCanvas.Children.Add(new ConcentratieGrafiek(ConcentratiePunten) { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch });

其中“ActieveCanvas”是画布。

有关信息:所有点都是画布内(相对于)的 X、Y 坐标。

我究竟做错了什么?

4

1 回答 1

0

通过更改以下内容来修复它

            if (PuntenCollectie.IndexOf(Punt).Equals(0))
            {
                cr.BeginFigure(Punt, false, false);
            }
            else
            {
                cr.LineTo(Punt, false, false);
            }

            if (PuntenCollectie.IndexOf(Punt).Equals(0))
            {
                cr.BeginFigure(Punt, true, false);
            }
            else
            {
                cr.LineTo(Punt, true, false);
            }
于 2015-11-12T16:01:54.177 回答